Skip to content

feat(auth): add captcha token to sign-in with password methods #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,29 +310,37 @@ public actor AuthClient {

/// Log in an existing user with an email and password.
@discardableResult
public func signIn(email: String, password: String) async throws -> Session {
public func signIn(email: String, password: String, captchaToken: String? = nil) async throws -> Session {
try await _signIn(
request: .init(
path: "/token",
method: .post,
query: [URLQueryItem(name: "grant_type", value: "password")],
body: configuration.encoder.encode(
UserCredentials(email: email, password: password)
UserCredentials(
email: email,
password: password,
gotrueMetaSecurity: captchaToken.map(AuthMetaSecurity.init(captchaToken:))
)
)
)
)
}

/// Log in an existing user with a phone and password.
@discardableResult
public func signIn(phone: String, password: String) async throws -> Session {
public func signIn(phone: String, password: String, captchaToken: String? = nil) async throws -> Session {
try await _signIn(
request: .init(
path: "/token",
method: .post,
query: [URLQueryItem(name: "grant_type", value: "password")],
body: configuration.encoder.encode(
UserCredentials(password: password, phone: phone)
UserCredentials(
password: password,
phone: phone,
gotrueMetaSecurity: captchaToken.map(AuthMetaSecurity.init(captchaToken:))
)
)
)
)
Expand Down
5 changes: 4 additions & 1 deletion Sources/Auth/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ public struct UserCredentials: Codable, Hashable, Sendable {
public var password: String?
public var phone: String?
public var refreshToken: String?
public var gotrueMetaSecurity: AuthMetaSecurity?

public init(
email: String? = nil,
password: String? = nil,
phone: String? = nil,
refreshToken: String? = nil
refreshToken: String? = nil,
gotrueMetaSecurity: AuthMetaSecurity? = nil
) {
self.email = email
self.password = password
self.phone = phone
self.refreshToken = refreshToken
self.gotrueMetaSecurity = gotrueMetaSecurity
}
}

Expand Down
6 changes: 4 additions & 2 deletions Tests/AuthTests/RequestsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ final class RequestsTests: XCTestCase {
await assert {
try await sut.signIn(
email: "[email protected]",
password: "the.pass"
password: "the.pass",
captchaToken: "dummy-captcha"
)
}
}
Expand All @@ -70,7 +71,8 @@ final class RequestsTests: XCTestCase {
await assert {
try await sut.signIn(
phone: "+1 202-918-2132",
password: "the.pass"
password: "the.pass",
captchaToken: "dummy-captcha"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ curl \
--header "Apikey: dummy.api.key" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--data "{\"email\":\"[email protected]\",\"password\":\"the.pass\"}" \
--data "{\"email\":\"[email protected]\",\"gotrue_meta_security\":{\"captcha_token\":\"dummy-captcha\"},\"password\":\"the.pass\"}" \
"http://localhost:54321/auth/v1/token?grant_type=password"
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ curl \
--header "Apikey: dummy.api.key" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--data "{\"password\":\"the.pass\",\"phone\":\"+1 202-918-2132\"}" \
--data "{\"gotrue_meta_security\":{\"captcha_token\":\"dummy-captcha\"},\"password\":\"the.pass\",\"phone\":\"+1 202-918-2132\"}" \
"http://localhost:54321/auth/v1/token?grant_type=password"