Skip to content

Commit 363aa00

Browse files
authored
feat(auth): add captcha token to sign-in with password methods (#276)
1 parent cac2433 commit 363aa00

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

Sources/Auth/AuthClient.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,29 +310,37 @@ public actor AuthClient {
310310

311311
/// Log in an existing user with an email and password.
312312
@discardableResult
313-
public func signIn(email: String, password: String) async throws -> Session {
313+
public func signIn(email: String, password: String, captchaToken: String? = nil) async throws -> Session {
314314
try await _signIn(
315315
request: .init(
316316
path: "/token",
317317
method: .post,
318318
query: [URLQueryItem(name: "grant_type", value: "password")],
319319
body: configuration.encoder.encode(
320-
UserCredentials(email: email, password: password)
320+
UserCredentials(
321+
email: email,
322+
password: password,
323+
gotrueMetaSecurity: captchaToken.map(AuthMetaSecurity.init(captchaToken:))
324+
)
321325
)
322326
)
323327
)
324328
}
325329

326330
/// Log in an existing user with a phone and password.
327331
@discardableResult
328-
public func signIn(phone: String, password: String) async throws -> Session {
332+
public func signIn(phone: String, password: String, captchaToken: String? = nil) async throws -> Session {
329333
try await _signIn(
330334
request: .init(
331335
path: "/token",
332336
method: .post,
333337
query: [URLQueryItem(name: "grant_type", value: "password")],
334338
body: configuration.encoder.encode(
335-
UserCredentials(password: password, phone: phone)
339+
UserCredentials(
340+
password: password,
341+
phone: phone,
342+
gotrueMetaSecurity: captchaToken.map(AuthMetaSecurity.init(captchaToken:))
343+
)
336344
)
337345
)
338346
)

Sources/Auth/Types.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ public struct UserCredentials: Codable, Hashable, Sendable {
2020
public var password: String?
2121
public var phone: String?
2222
public var refreshToken: String?
23+
public var gotrueMetaSecurity: AuthMetaSecurity?
2324

2425
public init(
2526
email: String? = nil,
2627
password: String? = nil,
2728
phone: String? = nil,
28-
refreshToken: String? = nil
29+
refreshToken: String? = nil,
30+
gotrueMetaSecurity: AuthMetaSecurity? = nil
2931
) {
3032
self.email = email
3133
self.password = password
3234
self.phone = phone
3335
self.refreshToken = refreshToken
36+
self.gotrueMetaSecurity = gotrueMetaSecurity
3437
}
3538
}
3639

Tests/AuthTests/RequestsTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ final class RequestsTests: XCTestCase {
5959
await assert {
6060
try await sut.signIn(
6161
62-
password: "the.pass"
62+
password: "the.pass",
63+
captchaToken: "dummy-captcha"
6364
)
6465
}
6566
}
@@ -70,7 +71,8 @@ final class RequestsTests: XCTestCase {
7071
await assert {
7172
try await sut.signIn(
7273
phone: "+1 202-918-2132",
73-
password: "the.pass"
74+
password: "the.pass",
75+
captchaToken: "dummy-captcha"
7476
)
7577
}
7678
}

Tests/AuthTests/__Snapshots__/RequestsTests/testSignInWithEmailAndPassword.1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ curl \
33
--header "Apikey: dummy.api.key" \
44
--header "Content-Type: application/json" \
55
--header "X-Client-Info: gotrue-swift/x.y.z" \
6-
--data "{\"email\":\"[email protected]\",\"password\":\"the.pass\"}" \
6+
--data "{\"email\":\"[email protected]\",\"gotrue_meta_security\":{\"captcha_token\":\"dummy-captcha\"},\"password\":\"the.pass\"}" \
77
"http://localhost:54321/auth/v1/token?grant_type=password"

Tests/AuthTests/__Snapshots__/RequestsTests/testSignInWithPhoneAndPassword.1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ curl \
33
--header "Apikey: dummy.api.key" \
44
--header "Content-Type: application/json" \
55
--header "X-Client-Info: gotrue-swift/x.y.z" \
6-
--data "{\"password\":\"the.pass\",\"phone\":\"+1 202-918-2132\"}" \
6+
--data "{\"gotrue_meta_security\":{\"captcha_token\":\"dummy-captcha\"},\"password\":\"the.pass\",\"phone\":\"+1 202-918-2132\"}" \
77
"http://localhost:54321/auth/v1/token?grant_type=password"

0 commit comments

Comments
 (0)