Overview
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
TurnkeyConfigandTurnkeyContext.configure(...).
Send an OTP code
Create or update your login screen to collect the user’s email. When the user submits the form, callinitOtp(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
Verify the OTP code
On your OTP screen, callcompleteOtp(...) with the user-entered code.This will verify the code and automatically complete login or signup as needed.
OtpScreen.swift
SMS OTP
For SMS, pass an E.164-formatted phone number and useotpType: .sms:
Notes
- Default OTP length is 6; if you’ve customized the OTP length in the dashboard, validate accordingly (up to 9 digits).
- To resend a code, call
initOtp(contact:otpType:)again with the same contact.