Skip to main content

Authentication flows

Use Auth for sign-in and sign-up flows, and to manage sessions. You can access auth with clerk.auth.

Sign in

Sign in with password

Sign in directly with an identifier (email, phone, or username) and password:

var signIn = try await clerk.auth.signInWithPassword(
  identifier: "user@email.com",
  password: "secretpassword"
)

Sign in with OTP (email)

Send an to the supplied email, and then verify the code.

var signIn = try await clerk.auth.signInWithEmailCode(emailAddress: "user@email.com")
signIn = try await signIn.verifyCode("123456")

Sign in with OTP (phone)

Send an to the supplied phone number, and then verify the code.

var signIn = try await clerk.auth.signInWithPhoneCode(phoneNumber: "+1234567890")
signIn = try await signIn.verifyCode("123456")

Sign in with OAuth

Sign in using an OAuth provider (e.g., Google, GitHub, see all providers):

var result = try await clerk.auth.signInWithOAuth(provider: .google)
var result = try await clerk.auth.signInWithApple()

See the Sign in with Apple guide for native iOS setup.

Sign in with ID token

Sign in with an ID token from a provider:

var result = try await clerk.auth.signInWithIdToken(
  idToken,
  provider: .apple
)

Sign in with passkey

Sign in using a passkey:

var signIn = try await clerk.auth.signInWithPasskey()
var result = try await clerk.auth.signInWithEnterpriseSSO(emailAddress: "user@company.com")

Sign in with ticket

Sign in using a ticket generated from the Backend API:

var signIn = try await clerk.auth.signInWithTicket(ticket)

If the Backend API created the sign-in token with org_id, Clerk activates that Organization for the resulting session. After sign-in succeeds, the Organization ID is available from Clerk.shared.session?.lastActiveOrganizationId.

Send an email link to the supplied email address. After the user opens the link, handle the callback with clerk.handle(url).

let signIn = try await clerk.auth.signInWithEmailLink(emailAddress: "user@email.com")
// Show a message that tells the user to check their email.

Sign in with identifier

Start a sign-in with an identifier (email, phone, or username):

// Email
var signIn = try await clerk.auth.signIn("user@email.com")
// Phone
var signIn = try await clerk.auth.signIn("+1234567890")
// Username
var signIn = try await clerk.auth.signIn("johndoe")

Continue an existing sign-in

Use these methods when you already have a SignIn from clerk.auth.signIn() or clerk.auth.currentSignIn, and want to complete it with a specific factor or provider.

Send and verify email code

Send the user an via email to verify their sign-in. Then verify the code that the user supplies:

signIn = try await signIn.sendEmailCode()
signIn = try await signIn.verifyCode("123456")

Send the user an email link to verify their sign-in. After the user opens the link, handle the callback with clerk.handle(url).

signIn = try await signIn.sendEmailLink()
// Show a message that tells the user to check their email.

Send and verify phone code

Send the user an via SMS to verify their sign-in. Then verify the code that the user supplies:

signIn = try await signIn.sendPhoneCode()
signIn = try await signIn.verifyCode("123456")
signIn = try await signIn.authenticateWithPassword("secretpassword")

Authenticate with OAuth

Authenticate with an OAuth provider (e.g., Google, GitHub, see all providers):

var result = try await signIn.authenticateWithOAuth(provider: .google)
var result = try await signIn.authenticateWithEnterpriseSSO()
var result = try await signIn.authenticateWithApple()
signIn = try await signIn.authenticateWithIdToken(
  idToken,
  provider: .apple
)
signIn = try await signIn.authenticateWithPasskey()

Multi-factor authentication

After completing first factor verification, if multi-factor authentication (MFA) is required, use these methods to complete the second factor verification. The examples assume you already have a SignIn from a sign-in flow.

MFA with phone code

Send and verify an MFA code via SMS:

signIn = try await signIn.sendMfaPhoneCode()
signIn = try await signIn.verifyMfaCode(
  "123456",
  type: .phoneCode
)

MFA with email code

Send and verify an MFA code via email:

signIn = try await signIn.sendMfaEmailCode()
signIn = try await signIn.verifyMfaCode(
  "123456",
  type: .emailCode
)

MFA with TOTP (authenticator app)

Verify using a TOTP code from an authenticator app:

signIn = try await signIn.verifyMfaCode(
  "123456",
  type: .totp
)

MFA with backup code

Verify using a backup code:

signIn = try await signIn.verifyMfaCode(
  "backup123",
  type: .backupCode
)

Password reset

The Clerk iOS SDK provides methods to reset user passwords via email or phone verification.

Password reset with email

Reset password using email verification:

var signIn = try await clerk.auth.signIn("user@email.com")
signIn = try await signIn.sendResetPasswordEmailCode()
signIn = try await signIn.verifyCode("123456")
signIn = try await signIn.resetPassword(
  newPassword: "newpassword",
  signOutOfOtherSessions: true
)

Password reset with phone

Reset password using phone verification:

var signIn = try await clerk.auth.signIn("+1234567890")
signIn = try await signIn.sendResetPasswordPhoneCode()
signIn = try await signIn.verifyCode("123456")
signIn = try await signIn.resetPassword(
  newPassword: "newpassword",
  signOutOfOtherSessions: true
)

Sign up

Create a new sign-up

Create a new account:

var signUp = try await clerk.auth.signUp(
  emailAddress: "newuser@email.com",
  phoneNumber: "+1234567890",
  password: "secretpassword",
  firstName: "John",
  lastName: "Doe",
  username: "johndoe",
  legalAccepted: true
)

Update a sign-up

Update sign-up information:

signUp = try await signUp.update(
  emailAddress: "newuser@email.com",
  phoneNumber: "+1234567890",
  password: "secretpassword",
  firstName: "John",
  lastName: "Doe",
  username: "johndoe",
  legalAccepted: true
)

Sign up with OAuth

Sign up using an OAuth provider (e.g., Google, GitHub, see all providers):

var result = try await clerk.auth.signUpWithOAuth(provider: .google)
var result = try await clerk.auth.signUpWithApple()

See the Sign in with Apple guide for native iOS setup.

Sign up with ID token

Sign up using an ID token:

var result = try await clerk.auth.signUpWithIdToken(
  idToken,
  provider: .apple,
  firstName: "John",
  lastName: "Doe"
)
var result = try await clerk.auth.signUpWithEnterpriseSSO(emailAddress: "user@company.com")

Sign up with ticket

Sign up using a ticket generated from the Backend API:

var signUp = try await clerk.auth.signUpWithTicket(ticket)

Continue an existing sign-up

Use these methods when you already have a SignUp from clerk.auth.signUp() or clerk.auth.currentSignUp and want to complete it with a specific factor or provider.

Send and verify email via OTP

Send the user an via email to verify their account. Then verify the code that the user supplies:

signUp = try await signUp.sendEmailCode()
signUp = try await signUp.verifyEmailCode("123456")

Send the user an email link to verify their account. After the user opens the link, handle the callback with clerk.handle(url).

signUp = try await signUp.sendEmailLink()
// Show a message that tells the user to check their email.

Send and verify phone via OTP

Send the user an via SMS to verify their account. Then verify the code that the user supplies:

signUp = try await signUp.sendPhoneCode()
signUp = try await signUp.verifyPhoneCode("123456")

Hosted authentication

Hosted authentication opens Clerk's Account Portal in a browser authentication session. Account Portal supports the sign-in and sign-up methods enabled for your Clerk application. When authentication completes, the SDK closes the browser session and activates the new session in your app.

The following example opens the Account Portal sign-in page:

try await clerk.auth.startHostedAuth()

Pass .signUp to open the sign-up page first:

try await clerk.auth.startHostedAuth(mode: .signUp)

startHostedAuth() accepts the following parameters:

  • mode - The Account Portal page to open first. Defaults to sign-in.
  • redirectUrl - A custom native callback with a non-HTTP URL scheme; http, https, and universal-link callbacks are rejected. Defaults to Clerk's configured redirect URL, which is <bundle-identifier>://callback unless overridden.
  • prefersEphemeralWebBrowserSession - Whether the browser should use an ephemeral session. Defaults to false.

startHostedAuth() returns the Session that the SDK activated. The result is discardable; clerk.user reflects the signed-in user after the call completes. If the user dismisses the browser or authentication fails, the method throws.

See the hosted authentication guide for setup instructions and tradeoffs.

Current sign-in/sign-up

Access the in-progress sign-in or sign-up stored on the client for multi-step flows.

Current sign-in

let currentSignIn = clerk.auth.currentSignIn
let currentSignUp = clerk.auth.currentSignUp

Session management

The Clerk iOS SDK provides methods to manage user sessions, including signing out, switching sessions, and revoking sessions.

Sessions

Retrieve all sessions for the current client:

let sessions = clerk.auth.sessions

Get session token

Retrieve the user's session token:

guard let token = try await clerk.auth.getToken() else { return }

Use this token when calling your backend to authenticate the request:

var request = URLRequest(url: url)
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")

Set current session

Set the current session and optionally the :

try await clerk.auth.setActive(
  sessionId: sessionId,
  organizationId: organizationId
)

Revoke session

Revoke a specific session:

let revokedSession = try await clerk.auth.revokeSession(session)

Sign out

Sign out the current user:

// Sign out from all sessions.
try await clerk.auth.signOut()

// Sign out from a specific session.
try await clerk.auth.signOut(sessionId: sessionId)

Deep linking

Handle OAuth, SSO, and email link deep link callbacks in your SwiftUI app:

.onOpenURL { url in
  Task {
    try? await clerk.handle(url)
  }
}

Feedback

What did you think of this content?

Last updated on