Skip to content

Commit

Permalink
feat(auth): add captcha token to sign-in with password methods (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Mar 22, 2024
1 parent cac2433 commit 363aa00
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
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: "example@mail.com",
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\":\"example@mail.com\",\"password\":\"the.pass\"}" \
--data "{\"email\":\"example@mail.com\",\"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"

0 comments on commit 363aa00

Please sign in to comment.