Skip to content

refactor: Separate OTPType into mobile and email #142

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
Nov 2, 2023
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
32 changes: 18 additions & 14 deletions Sources/GoTrue/GoTrueClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ public actor GoTrueClient {
public func verifyOTP(
email: String,
token: String,
type: OTPType,
type: EmailOTPType,
redirectTo: URL? = nil,
captchaToken: String? = nil
) async throws -> AuthResponse {
Expand All @@ -621,15 +621,17 @@ public actor GoTrueClient {
redirectTo.map { URLQueryItem(name: "redirect_to", value: $0.absoluteString) },
].compactMap { $0 },
body: configuration.encoder.encode(
VerifyOTPParams(
email: email,
token: token,
type: type,
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:))
VerifyOTPParams.email(
VerifyEmailOTPParams(
email: email,
token: token,
type: type,
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:))
)
)
)
),
shouldRemoveSession: type != .emailChange && type != .phoneChange
shouldRemoveSession: type != .emailChange
)
}

Expand All @@ -638,23 +640,25 @@ public actor GoTrueClient {
public func verifyOTP(
phone: String,
token: String,
type: OTPType,
type: MobileOTPType,
captchaToken: String? = nil
) async throws -> AuthResponse {
try await _verifyOTP(
request: .init(
path: "/verify",
method: .post,
body: configuration.encoder.encode(
VerifyOTPParams(
phone: phone,
token: token,
type: type,
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:))
VerifyOTPParams.mobile(
VerifyMobileOTPParams(
phone: phone,
token: token,
type: type,
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:))
)
)
)
),
shouldRemoveSession: type != .emailChange && type != .phoneChange
shouldRemoveSession: type != .phoneChange
)
}

Expand Down
35 changes: 30 additions & 5 deletions Sources/GoTrue/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,22 +273,47 @@ struct OTPParams: Codable, Hashable, Sendable {
var codeChallengeMethod: String?
}

struct VerifyOTPParams: Codable, Hashable, Sendable {
var email: String?
var phone: String?
enum VerifyOTPParams: Encodable {
case email(VerifyEmailOTPParams)
case mobile(VerifyMobileOTPParams)

func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .email(value):
try container.encode(value)
case let .mobile(value):
try container.encode(value)
}
}
}

struct VerifyEmailOTPParams: Encodable, Hashable, Sendable {
var email: String
var token: String
var type: EmailOTPType
var gotrueMetaSecurity: GoTrueMetaSecurity?
}

struct VerifyMobileOTPParams: Encodable, Hashable {
var phone: String
var token: String
var type: OTPType
var type: MobileOTPType
var gotrueMetaSecurity: GoTrueMetaSecurity?
}

public enum OTPType: String, Codable, CaseIterable, Sendable {
public enum MobileOTPType: String, Encodable, Sendable {
case sms
case phoneChange = "phone_change"
}

public enum EmailOTPType: String, Encodable, CaseIterable, Sendable {
case signup
case invite
case magiclink
case recovery
case emailChange = "email_change"
case email
}

public enum AuthResponse: Codable, Hashable, Sendable {
Expand Down