This guide shows how to implement email or SMS OTP authentication using the Turnkey Swift SDK and the Auth Proxy.
You’ll send an OTP to a user’s contact, present a verification screen, and complete the flow with a single call that handles login or signup.Before you begin:
Ensure you’ve completed the setup in Getting Started and enabled the Auth Proxy with Email/SMS OTP in the Turnkey Dashboard.
Make sure your app is configured with TurnkeyConfig and TurnkeyContext.configure(...).
Create or update your login screen to collect the user’s email. When the user submits the form, call initOtp(contact:otpType:) with the email and .email.
This sends a one-time code to the provided address.On success, keep the returned otpId along with the email so you can navigate to your OTP screen. In the next section, you’ll verify the code the user enters.
LoginView.swift
import SwiftUIimport TurnkeySwiftstruct LoginView: View { @EnvironmentObject var turnkey: TurnkeyContext @State private var email = "" @State private var errorMessage: String? var body: some View { VStack(spacing: 12) { TextField("you@example.com", text: $email) .keyboardType(.emailAddress) .textInputAutocapitalization(.never) .autocorrectionDisabled() .frame(height: 48) .overlay(RoundedRectangle(cornerRadius: 8).stroke(.gray.opacity(0.3))) Button("Continue with email") { Task { guard email.contains("@") else { errorMessage = "Please enter a valid email" return } do { let result = try await turnkey.initOtp(contact: email, otpType: .email) // Navigate to your OTP screen with `email` and `result.otpId` // Example: OtpScreen(email: email, otpId: result.otpId) } catch { errorMessage = "Failed to initialize OTP" } } } .disabled(!email.contains("@")) } .padding() .alert("Error", isPresented: .constant(errorMessage != nil)) { Button("OK") { errorMessage = nil } } message: { Text(errorMessage ?? "") } }}