sendEmailOtp

Send a one-time password (OTP) to a user’s email address for authentication.
public func sendEmailOtp(
    client: DynamicClient,
    email: String
) async throws -> OTPVerification

Parameters

  • client (DynamicClient) - Initialized Dynamic client
  • email (String) - User’s email address

Returns

  • OTPVerification - Verification object containing the verification UUID

Example

let otpVerification = try await sendEmailOtp(
    client: dynamicClient,
    email: "[email protected]"
)

// Store for verification step
var currentOtpVerification: OTPVerification?
currentOtpVerification = otpVerification

sendSmsOtp

Send a one-time password (OTP) to a user’s phone number via SMS.
public func sendSmsOtp(
    client: DynamicClient,
    phoneNumber: String,
    phoneCountryCode: String,
    isoCountryCode: String
) async throws -> OTPVerification

Parameters

  • client (DynamicClient) - Initialized Dynamic client
  • phoneNumber (String) - User’s phone number (without country code)
  • phoneCountryCode (String) - Country code (e.g., “+1”)
  • isoCountryCode (String) - ISO country code (e.g., “US”)

Returns

  • OTPVerification - Verification object containing the verification UUID

Example

let otpVerification = try await sendSmsOtp(
    client: dynamicClient,
    phoneNumber: "1234567890",
    phoneCountryCode: "+1",
    isoCountryCode: "US"
)

// Store for verification step
var currentOtpVerification: OTPVerification?
currentOtpVerification = otpVerification

verifyOtp

Verify an email OTP code and authenticate the user.
public func verifyOtp(
    otpVerification: OTPVerification,
    verificationToken: String
) async throws -> SdkUser

Parameters

  • otpVerification (OTPVerification) - Verification object from sendEmailOtp
  • verificationToken (String) - OTP code entered by user

Returns

  • SdkUser - Authenticated user object

Example

guard let otpVerification = currentOtpVerification else {
    print("No OTP verification in progress")
    return
}

do {
    let authenticatedUser = try await verifyOtp(
        otpVerification: otpVerification,
        verificationToken: "123456"
    )
    
    print("Welcome \(authenticatedUser.email ?? "")")
} catch {
    print("Invalid OTP: \(error)")
}

verifySmsOtp

Verify an SMS OTP code and authenticate the user.
public func verifySmsOtp(
    otpVerification: OTPVerification,
    verificationToken: String
) async throws -> SdkUser

Parameters

  • otpVerification (OTPVerification) - Verification object from sendSmsOtp
  • verificationToken (String) - OTP code entered by user

Returns

  • SdkUser - Authenticated user object

Example

guard let otpVerification = currentOtpVerification else {
    print("No OTP verification in progress")
    return
}

do {
    let authenticatedUser = try await verifySmsOtp(
        otpVerification: otpVerification,
        verificationToken: "123456"
    )
    
    print("Welcome user with phone: \(authenticatedUser.phoneNumber ?? "")")
} catch {
    print("Invalid OTP: \(error)")
}

logout

Clear the current authentication state and log out the user.
public func logout(client: DynamicClient) async throws

Parameters

  • client (DynamicClient) - Initialized Dynamic client

Example

do {
    try await logout(client: dynamicClient)
    print("User logged out successfully")
} catch {
    print("Logout failed: \(error)")
}

Authentication Flow

Complete Email Authentication Flow

import DynamicSwiftSDK

let dynamicClient: DynamicClient
let userEmail: String = "[email protected]"

// Step 1: Send OTP
let otpVerification = try await sendEmailOtp(
    client: dynamicClient,
    email: userEmail
)

// Step 2: Verify OTP (when user enters code)
let otpCode: String = "123456" // User input
let authenticatedUser = try await verifyOtp(
    otpVerification: otpVerification,
    verificationToken: otpCode
)

print("Welcome \(authenticatedUser.email ?? "")")

Complete SMS Authentication Flow

import DynamicSwiftSDK

let dynamicClient: DynamicClient
let phoneNumber: String = "1234567890"
let phoneCountryCode: String = "+1"
let isoCountryCode: String = "US"

// Step 1: Send OTP
let otpVerification = try await sendSmsOtp(
    client: dynamicClient,
    phoneNumber: phoneNumber,
    phoneCountryCode: phoneCountryCode,
    isoCountryCode: isoCountryCode
)

// Step 2: Verify OTP (when user enters code)
let otpCode: String = "123456" // User input
let authenticatedUser = try await verifySmsOtp(
    otpVerification: otpVerification,
    verificationToken: otpCode
)

print("Welcome user with phone: \(authenticatedUser.phoneNumber ?? "")")