> ## Documentation Index
> Fetch the complete documentation index at: https://www.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication Functions

## sdk.auth.email

Email OTP authentication methods.

### sendOTP

Send a one-time password (OTP) to a user's email address for authentication.

```swift theme={"system"}
try await sdk.auth.email.sendOTP(email: String)
```

#### Parameters

* **email** (String) - User's email address

#### Example

```swift theme={"system"}
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.

```swift theme={"system"}
try await sdk.auth.email.verifyOTP(token: String)
```

#### Parameters

* **token** (String) - OTP code entered by user

#### Example

```swift theme={"system"}
let sdk = DynamicSDK.instance()

do {
    try await sdk.auth.email.verifyOTP(token: "123456")
    print("Email verified successfully!")
    // User is now authenticated
    // Access authenticated user via: sdk.auth.authenticatedUser
    if let user = sdk.auth.authenticatedUser {
        print("Welcome \(user.email ?? "")")
    }
} catch {
    print("Invalid OTP: \(error)")
}
```

### resendOTP

Resend the email OTP.

```swift theme={"system"}
try await sdk.auth.email.resendOTP()
```

#### Example

```swift theme={"system"}
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.

```swift theme={"system"}
try await sdk.auth.sms.sendOTP(phoneData: PhoneData)
```

#### Parameters

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

#### Example

```swift theme={"system"}
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.

```swift theme={"system"}
try await sdk.auth.sms.verifyOTP(token: String)
```

#### Parameters

* **token** (String) - OTP code entered by user

#### Example

```swift theme={"system"}
let sdk = DynamicSDK.instance()

do {
    try await sdk.auth.sms.verifyOTP(token: "123456")
    print("Phone verified successfully!")
    // User is now authenticated
    if let user = sdk.auth.authenticatedUser {
        print("Welcome user with phone: \(user.phoneNumber ?? "")")
    }
} catch {
    print("Invalid OTP: \(error)")
}
```

### resendOTP

Resend the SMS OTP.

```swift theme={"system"}
try await sdk.auth.sms.resendOTP()
```

#### Example

```swift theme={"system"}
let sdk = DynamicSDK.instance()

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.

```swift theme={"system"}
try await sdk.auth.social.connect(provider: SocialProvider)
```

#### Parameters

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

#### Example

```swift theme={"system"}
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.

```swift theme={"system"}
try await sdk.auth.passkey.signIn()
```

#### Example

```swift theme={"system"}
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.

```swift theme={"system"}
try await sdk.auth.externalAuth.signInWithExternalJwt(props: SignInWithExternalJwtParams)
```

#### Parameters

* **props** (SignInWithExternalJwtParams) - Parameters containing the JWT token

#### Example

```swift theme={"system"}
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.

```swift theme={"system"}
let user = sdk.auth.authenticatedUser  // UserProfile?
```

#### Example

```swift theme={"system"}
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.

```swift theme={"system"}
sdk.auth.authenticatedUserChanges  // Publisher<UserProfile?, Never>
```

#### Example

```swift theme={"system"}
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.

```swift theme={"system"}
let token = sdk.auth.token  // String?
```

### tokenChanges

Combine publisher for token changes.

```swift theme={"system"}
sdk.auth.tokenChanges  // Publisher<String?, Never>
```

## Logout

### logout

Log out the current user.

```swift theme={"system"}
try await sdk.auth.logout()
```

#### Example

```swift theme={"system"}
let sdk = DynamicSDK.instance()

do {
    try await sdk.auth.logout()
    print("Logged out successfully")
    // Authentication state automatically updates via authenticatedUserChanges publisher
} catch {
    print("Logout failed: \(error)")
}
```

## Complete Authentication Flow

### Email Authentication Flow

```swift theme={"system"}
import DynamicSDKSwift

let sdk = DynamicSDK.instance()
let userEmail: String = "user@example.com"

// Step 1: Send OTP
do {
    try await sdk.auth.email.sendOTP(email: userEmail)

    // Step 2: Verify OTP (when user enters code)
    let otpCode: String = "123456" // User input
    try await sdk.auth.email.verifyOTP(token: otpCode)

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

### SMS Authentication Flow

```swift theme={"system"}
import DynamicSDKSwift

let sdk = DynamicSDK.instance()
let phoneData = PhoneData(dialCode: "+1", iso2: "US", phone: "5551234567")

// Step 1: Send OTP
do {
    try await sdk.auth.sms.sendOTP(phoneData: phoneData)

    // Step 2: Verify OTP (when user enters code)
    let otpCode: String = "123456" // User input
    try await sdk.auth.sms.verifyOTP(token: otpCode)

    // User is now authenticated
    if let user = sdk.auth.authenticatedUser {
        print("Welcome user with phone: \(user.phoneNumber ?? "")")
    }
} catch {
    print("Authentication failed: \(error)")
}
```

### Social Authentication

```swift theme={"system"}
let sdk = DynamicSDK.instance()

// Sign in with Google
try await sdk.auth.social.connect(provider: .google)

// Sign in with Apple
try await sdk.auth.social.connect(provider: .apple)

// Sign in with Farcaster
try await sdk.auth.social.connect(provider: .farcaster)
```

### Passkey Authentication

```swift theme={"system"}
let sdk = DynamicSDK.instance()

// Sign in with passkey
do {
    _ = try await sdk.auth.passkey.signIn()
    // User is now authenticated
} catch {
    print("Passkey sign-in failed: \(error)")
}
```

### External JWT Authentication

```swift theme={"system"}
let sdk = DynamicSDK.instance()

do {
    try await sdk.auth.externalAuth.signInWithExternalJwt(
        props: SignInWithExternalJwtParams(jwt: "your-jwt-token")
    )
    // User is now authenticated
} catch {
    print("External JWT sign-in failed: \(error)")
}
```
