Skip to content

Commit 32e426e

Browse files
authored
refactor: Separate OTPType into mobile and email (#142)
1 parent 0c8ffcc commit 32e426e

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

Sources/GoTrue/GoTrueClient.swift

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ public actor GoTrueClient {
609609
public func verifyOTP(
610610
email: String,
611611
token: String,
612-
type: OTPType,
612+
type: EmailOTPType,
613613
redirectTo: URL? = nil,
614614
captchaToken: String? = nil
615615
) async throws -> AuthResponse {
@@ -621,15 +621,17 @@ public actor GoTrueClient {
621621
redirectTo.map { URLQueryItem(name: "redirect_to", value: $0.absoluteString) },
622622
].compactMap { $0 },
623623
body: configuration.encoder.encode(
624-
VerifyOTPParams(
625-
email: email,
626-
token: token,
627-
type: type,
628-
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:))
624+
VerifyOTPParams.email(
625+
VerifyEmailOTPParams(
626+
email: email,
627+
token: token,
628+
type: type,
629+
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:))
630+
)
629631
)
630632
)
631633
),
632-
shouldRemoveSession: type != .emailChange && type != .phoneChange
634+
shouldRemoveSession: type != .emailChange
633635
)
634636
}
635637

@@ -638,23 +640,25 @@ public actor GoTrueClient {
638640
public func verifyOTP(
639641
phone: String,
640642
token: String,
641-
type: OTPType,
643+
type: MobileOTPType,
642644
captchaToken: String? = nil
643645
) async throws -> AuthResponse {
644646
try await _verifyOTP(
645647
request: .init(
646648
path: "/verify",
647649
method: .post,
648650
body: configuration.encoder.encode(
649-
VerifyOTPParams(
650-
phone: phone,
651-
token: token,
652-
type: type,
653-
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:))
651+
VerifyOTPParams.mobile(
652+
VerifyMobileOTPParams(
653+
phone: phone,
654+
token: token,
655+
type: type,
656+
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:))
657+
)
654658
)
655659
)
656660
),
657-
shouldRemoveSession: type != .emailChange && type != .phoneChange
661+
shouldRemoveSession: type != .phoneChange
658662
)
659663
}
660664

Sources/GoTrue/Types.swift

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,22 +273,47 @@ struct OTPParams: Codable, Hashable, Sendable {
273273
var codeChallengeMethod: String?
274274
}
275275

276-
struct VerifyOTPParams: Codable, Hashable, Sendable {
277-
var email: String?
278-
var phone: String?
276+
enum VerifyOTPParams: Encodable {
277+
case email(VerifyEmailOTPParams)
278+
case mobile(VerifyMobileOTPParams)
279+
280+
func encode(to encoder: Encoder) throws {
281+
var container = encoder.singleValueContainer()
282+
switch self {
283+
case let .email(value):
284+
try container.encode(value)
285+
case let .mobile(value):
286+
try container.encode(value)
287+
}
288+
}
289+
}
290+
291+
struct VerifyEmailOTPParams: Encodable, Hashable, Sendable {
292+
var email: String
293+
var token: String
294+
var type: EmailOTPType
295+
var gotrueMetaSecurity: GoTrueMetaSecurity?
296+
}
297+
298+
struct VerifyMobileOTPParams: Encodable, Hashable {
299+
var phone: String
279300
var token: String
280-
var type: OTPType
301+
var type: MobileOTPType
281302
var gotrueMetaSecurity: GoTrueMetaSecurity?
282303
}
283304

284-
public enum OTPType: String, Codable, CaseIterable, Sendable {
305+
public enum MobileOTPType: String, Encodable, Sendable {
285306
case sms
286307
case phoneChange = "phone_change"
308+
}
309+
310+
public enum EmailOTPType: String, Encodable, CaseIterable, Sendable {
287311
case signup
288312
case invite
289313
case magiclink
290314
case recovery
291315
case emailChange = "email_change"
316+
case email
292317
}
293318

294319
public enum AuthResponse: Codable, Hashable, Sendable {

0 commit comments

Comments
 (0)