Skip to content

Commit 58ab9af

Browse files
authored
fix(auth): verify otp using token hash (#451)
1 parent 58a55c9 commit 58ab9af

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

Examples/Examples/Profile/ProfileView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ struct ProfileView: View {
1919
NavigationStack {
2020
List {
2121
if let user,
22-
let json = try? AnyJSON(user) {
22+
let json = try? AnyJSON(user)
23+
{
2324
Section {
2425
AnyJSONView(value: json)
2526
}

Examples/Examples/Profile/UpdateProfileView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// Created by Guilherme Souza on 14/05/24.
66
//
77

8-
import SwiftUI
98
import Supabase
9+
import SwiftUI
1010

1111
struct UpdateProfileView: View {
1212
let user: User
@@ -93,7 +93,7 @@ struct UpdateProfileView: View {
9393
}
9494

9595
@MainActor
96-
private func verifyTapped() async {
96+
private func verifyTapped() async {
9797
do {
9898
try await supabase.auth.verifyOTP(phone: phone, token: otp, type: .phoneChange)
9999
} catch {

Sources/Auth/AuthClient.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,25 @@ public final class AuthClient: Sendable {
862862
)
863863
}
864864

865+
/// Log in an user given a token hash received via email.
866+
@discardableResult
867+
public func verifyOTP(
868+
tokenHash: String,
869+
type: EmailOTPType
870+
) async throws -> AuthResponse {
871+
try await _verifyOTP(
872+
request: .init(
873+
url: configuration.url.appendingPathComponent("verify"),
874+
method: .post,
875+
body: configuration.encoder.encode(
876+
VerifyOTPParams.tokenHash(
877+
VerifyTokenHashParams(tokenHash: tokenHash, type: type)
878+
)
879+
)
880+
)
881+
)
882+
}
883+
865884
private func _verifyOTP(request: HTTPRequest) async throws -> AuthResponse {
866885
let response = try await api.execute(request).decoded(
867886
as: AuthResponse.self,

Sources/Auth/Types.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ struct OTPParams: Codable, Hashable, Sendable {
371371
enum VerifyOTPParams: Encodable {
372372
case email(VerifyEmailOTPParams)
373373
case mobile(VerifyMobileOTPParams)
374+
case tokenHash(VerifyTokenHashParams)
374375

375376
func encode(to encoder: any Encoder) throws {
376377
var container = encoder.singleValueContainer()
@@ -379,6 +380,8 @@ enum VerifyOTPParams: Encodable {
379380
try container.encode(value)
380381
case let .mobile(value):
381382
try container.encode(value)
383+
case let .tokenHash(value):
384+
try container.encode(value)
382385
}
383386
}
384387
}
@@ -390,6 +393,11 @@ struct VerifyEmailOTPParams: Encodable, Hashable, Sendable {
390393
var gotrueMetaSecurity: AuthMetaSecurity?
391394
}
392395

396+
struct VerifyTokenHashParams: Encodable, Hashable, Sendable {
397+
var tokenHash: String
398+
var type: EmailOTPType
399+
}
400+
393401
struct VerifyMobileOTPParams: Encodable, Hashable {
394402
var phone: String
395403
var token: String

Tests/AuthTests/RequestsTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ final class RequestsTests: XCTestCase {
273273
}
274274
}
275275

276+
func testVerifyOTPUsingTokenHash() async {
277+
let sut = makeSUT()
278+
279+
await assert {
280+
try await sut.verifyOTP(
281+
tokenHash: "abc-def",
282+
type: .email
283+
)
284+
}
285+
}
286+
276287
func testUpdateUser() async throws {
277288
let sut = makeSUT()
278289

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
curl \
2+
--request POST \
3+
--header "Apikey: dummy.api.key" \
4+
--header "Content-Type: application/json" \
5+
--header "X-Client-Info: gotrue-swift/x.y.z" \
6+
--data "{\"token_hash\":\"abc-def\",\"type\":\"email\"}" \
7+
"http://localhost:54321/auth/v1/verify"

0 commit comments

Comments
 (0)