Skip to main content

sdk.auth.email

Email OTP authentication methods.

sendOTP

Send a one-time password (OTP) to a user’s email address for authentication.
try await sdk.auth.email.sendOTP(email: String)

Parameters

  • email (String) - User’s email address

Example

let sdk = DynamicSDK.instance()

do {
    try await sdk.auth.email.sendOTP(email: "user@example.com")
    print("OTP sent successfully")
    // Show OTP input UI
} catch {
    print("Failed to send OTP: \(error)")
}

verifyOTP

Verify an email OTP code and authenticate the user.
try await sdk.auth.email.verifyOTP(token: String)

Parameters

  • token (String) - OTP code entered by user

Example

do {
    try await sdk.auth.email.verifyOTP(token: "123456")
    print("Email verified successfully!")
    // User is now authenticated
} catch {
    print("Invalid OTP: \(error)")
}

resendOTP

Resend the email OTP.
try await sdk.auth.email.resendOTP()

Example

do {
    try await sdk.auth.email.resendOTP()
    print("OTP resent")
} catch {
    print("Failed to resend OTP: \(error)")
}

sdk.auth.sms

SMS OTP authentication methods.

sendOTP

Send a one-time password (OTP) to a user’s phone number via SMS.
try await sdk.auth.sms.sendOTP(phoneData: PhoneData)

Parameters

  • phoneData (PhoneData) - Phone data containing dial code, ISO code, and phone number

Example

let sdk = DynamicSDK.instance()

let phoneData = PhoneData(
    dialCode: "+1",      // Country dial code
    iso2: "US",          // ISO country code
    phone: "5551234567"  // Phone number without country code
)

do {
    try await sdk.auth.sms.sendOTP(phoneData: phoneData)
    print("SMS OTP sent")
    // Show OTP input UI
} catch {
    print("Failed to send SMS OTP: \(error)")
}

verifyOTP

Verify an SMS OTP code and authenticate the user.
try await sdk.auth.sms.verifyOTP(token: String)

Parameters

  • token (String) - OTP code entered by user

Example

do {
    try await sdk.auth.sms.verifyOTP(token: "123456")
    print("Phone verified successfully!")
    // User is now authenticated
} catch {
    print("Invalid OTP: \(error)")
}

resendOTP

Resend the SMS OTP.
try await sdk.auth.sms.resendOTP()

Example

do {
    try await sdk.auth.sms.resendOTP()
    print("SMS OTP resent")
} catch {
    print("Failed to resend OTP: \(error)")
}

sdk.auth.social

Social authentication methods.

connect

Authenticate with a social provider.
try await sdk.auth.social.connect(provider: SocialProvider)

Parameters

  • provider (SocialProvider) - Social provider to authenticate with (.google, .apple, .farcaster)

Example

let sdk = DynamicSDK.instance()

// Google Sign-In
do {
    try await sdk.auth.social.connect(provider: .google)
    print("Signed in with Google!")
} catch {
    print("Google sign-in failed: \(error)")
}

// Apple Sign-In
do {
    try await sdk.auth.social.connect(provider: .apple)
    print("Signed in with Apple!")
} catch {
    print("Apple sign-in failed: \(error)")
}

// Farcaster Sign-In
do {
    try await sdk.auth.social.connect(provider: .farcaster)
    print("Signed in with Farcaster!")
} catch {
    print("Farcaster sign-in failed: \(error)")
}

sdk.auth.passkey

Passkey authentication methods.

signIn

Sign in with a passkey.
try await sdk.auth.passkey.signIn()

Example

let sdk = DynamicSDK.instance()

do {
    _ = try await sdk.auth.passkey.signIn()
    print("Signed in with passkey!")
} catch {
    print("Passkey sign-in failed: \(error)")
}

sdk.auth.externalAuth

External JWT authentication.

signInWithExternalJwt

Authenticate using an external JWT token.
try await sdk.auth.externalAuth.signInWithExternalJwt(props: SignInWithExternalJwtParams)

Parameters

  • props (SignInWithExternalJwtParams) - Parameters containing the JWT token

Example

let sdk = DynamicSDK.instance()

do {
    try await sdk.auth.externalAuth.signInWithExternalJwt(
        props: SignInWithExternalJwtParams(jwt: "your-jwt-token")
    )
    print("Signed in with external JWT!")
} catch {
    print("External JWT sign-in failed: \(error)")
}

Authentication State

authenticatedUser

Get the current authenticated user.
let user = sdk.auth.authenticatedUser  // UserProfile?

Example

let sdk = DynamicSDK.instance()

if let user = sdk.auth.authenticatedUser {
    print("User ID: \(user.userId)")
    print("Email: \(user.email ?? "Not provided")")
} else {
    print("Not authenticated")
}

authenticatedUserChanges

Combine publisher for authentication state changes.
sdk.auth.authenticatedUserChanges  // Publisher<UserProfile?, Never>

Example

import Combine

let sdk = DynamicSDK.instance()
var cancellables = Set<AnyCancellable>()

sdk.auth.authenticatedUserChanges
    .receive(on: DispatchQueue.main)
    .sink { user in
        if let user = user {
            print("User logged in: \(user.userId)")
        } else {
            print("User logged out")
        }
    }
    .store(in: &cancellables)

token

Get the current authentication token.
let token = sdk.auth.token  // String?

tokenChanges

Combine publisher for token changes.
sdk.auth.tokenChanges  // Publisher<String?, Never>

Logout

logout

Log out the current user.
try await sdk.auth.logout()

Example

let sdk = DynamicSDK.instance()

do {
    try await sdk.auth.logout()
    print("Logged out successfully")
} catch {
    print("Logout failed: \(error)")
}

Complete Authentication Flow

Email Authentication Flow

import DynamicSDKSwift

let sdk = DynamicSDK.instance()

// Step 1: Send OTP
try await sdk.auth.email.sendOTP(email: "user@example.com")

// Step 2: Verify OTP (when user enters code)
try await sdk.auth.email.verifyOTP(token: "123456")

// User is now authenticated
if let user = sdk.auth.authenticatedUser {
    print("Welcome \(user.email ?? "")!")
}

SMS Authentication Flow

import DynamicSDKSwift

let sdk = DynamicSDK.instance()

// Step 1: Send OTP
let phoneData = PhoneData(dialCode: "+1", iso2: "US", phone: "5551234567")
try await sdk.auth.sms.sendOTP(phoneData: phoneData)

// Step 2: Verify OTP (when user enters code)
try await sdk.auth.sms.verifyOTP(token: "123456")

// User is now authenticated
if let user = sdk.auth.authenticatedUser {
    print("Welcome!")
}