Skip to main content
Docs

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):

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

Sign in with Apple

Sign in using Sign in with Apple:

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)

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 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 OAuth (e.g., Google, GitHub):

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

Sign up with Apple

Sign up using sign in with Apple:

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 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")

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)

Feedback

What did you think of this content?

Last updated on

GitHubEdit on GitHub