Utilize our Headless SDK for ultimate flexibility. This guide provides detailed instructions on integrating custom UI elements with OTPLESS’s backend authentication functions.
This is the new Headless authentication SDK that is significantly faster and more robust than the previous version. This upgrade enhances performance, reliability, and security, ensuring a seamless authentication experience, along with a seamless integration process. We strongly recommend migrating to the new SDK for improved efficiency and better support. To migrate from the old SDK, remove the previous SDK dependency and integration and follow the below mentioned steps.
Conform to OtplessResponseDelegate in your signup/sign in file to receive callbacks from OtplessBM.
funconResponse(_ response:OtplessBM.OtplessResponse){Otpless.shared.commitOtplessResponse(response)switch response.responseType {case.INITIATE:// Notify that authentication has been initiatedif response.statusCode ==200{print("Authentication initiated")}else{handleInitiateError(response)}case.VERIFY:// Notify that verification has failedhandleVerifyError(response)case.ONETAP:// Final response with tokenif response.statusCode ==200{iflet data = response.response?["data"]as?[String:Any],let token = data["token"]as?String,!token.isEmpty {// Process token and proceed to verify the token on your backend.print("Received token: \(token)")}else{print("Token not received")}}else{print("Token verification failed")}case.FALLBACK_TRIGGERED:// A fallback occurs when an OTP delivery attempt on one channel fails, // and the system automatically retries via the subsequent channel selected on Otpless Dashboard. // For example, if a merchant opts for SmartAuth with primary channal as WhatsApp and secondary channel as SMS,// in that case, if OTP delivery on WhatsApp fails, the system will automatically retry via SMS.// The response will contain the deliveryChannel to which the OTP has been sent.let newDeliveryChannel = response.response?["deliveryChannel"]as?String// This is the deliveryChannel to which the OTP has been sentprint("Fallback authentication triggered")case.SDK_READY:// Notify that SDK has been initialized successfullyprint("SDK has been initialized successfully, you may enable your continue button or proceed with user authentication.")case.FAILED:// Notify that the initialization has failedif response.statusCode ==5003{print("SDK initialization failed, please try to initialize the SDK again")}}}
funchandleInitiateError(_ response:OtplessResponse){guardlet responseDict = response.response as?[String:Any]else{return}let errorCode = responseDict["errorCode"]as?Stringlet errorMessage = responseDict["errorMessage"]as?Stringswitch errorCode {case"7101":print("OTPless Error: Invalid parameters values or missing parameters - \(errorMessage ??"Unknown error")")case"7102":print("OTPless Error: Invalid phone number - \(errorMessage ??"Unknown error")")case"7103":print("OTPless Error: Invalid phone number delivery channel - \(errorMessage ??"Unknown error")")case"7104":print("OTPless Error: Invalid email - \(errorMessage ??"Unknown error")")case"7105":print("OTPless Error: Invalid email channel - \(errorMessage ??"Unknown error")")case"7106":print("OTPless Error: Invalid phone number or email - \(errorMessage ??"Unknown error")")case"7113":print("OTPless Error: Invalid expiry - \(errorMessage ??"Unknown error")")case"7116":print("OTPless Error: OTP Length is invalid (4 or 6 only allowed) - \(errorMessage ??"Unknown error")")case"7121":print("OTPless Error: Invalid app hash - \(errorMessage ??"Unknown error")")case"4000":print("OTPless Error: Invalid request values - \(errorMessage ??"Unknown error")")case"4001":print("OTPless Error: Unsupported 2FA request - \(errorMessage ??"Unknown error")")case"4003":print("OTPless Error: Incorrect request channel - \(errorMessage ??"Unknown error")")case"401","7025":print("OTPless Error: Unauthorized request or country not enabled - \(errorMessage ??"Unknown error")")case"7020","7022","7023","7024":print("OTPless Error: Rate limiting error (Too many requests) - \(errorMessage ??"Unknown error")")// Feature not supported on older iOS versionscase"5900":print("OTPless Error: The feature is not supported because it requires a newer iOS version - \(errorMessage ??"The requested feature is only available on newer iOS versions.")")// Internet-related errorscase"9100":print("OTPless Error: Request Timeout - \(errorMessage ??"The request took too long to complete and was aborted.")")case"9101":print("OTPless Error: Network Connection Was Lost - \(errorMessage ??"The connection was interrupted before the request could complete.")")case"9102":print("OTPless Error: DNS Lookup Failed - \(errorMessage ??"The domain name could not be resolved, possibly due to network issues.")")case"9103":print("OTPless Error: Cannot Connect to Server - \(errorMessage ??"The server is unreachable, possibly due to downtime or incorrect configurations.")")case"9104":print("OTPless Error: No Internet Connection - \(errorMessage ??"The device is not connected to the internet.")")case"9105":print("OTPless Error: Secure Connection Failed - \(errorMessage ??"A secure connection could not be established due to SSL/TLS issues.")")case"9110":print("OTPless Error: Otpless Authentication Request Cancelled - \(errorMessage ??"The authentication request was manually canceled by the user or the system.")")default:print("OTPless Error: \(errorMessage ??"Unknown error")")}}
funchandleVerifyError(response:OtplessResponse){guardlet responseDict = response.response as?[String:Any]else{return}let errorCode = responseDict["errorCode"]as?Stringlet errorMessage = responseDict["errorMessage"]as?Stringswitch errorCode {case"7112":// Handle request error: Empty OTPprint("OTPless Error: \(errorMessage ??"Unknown error")")case"7115":// Handle request error: OTP is already verifiedprint("OTPless Error: \(errorMessage ??"Unknown error")")case"7118":// Handle request error: Incorrect OTPprint("OTPless Error: \(errorMessage ??"Unknown error")")case"7303":// Handle request error: OTP expiredprint("OTPless Error: \(errorMessage ??"Unknown error")")case"4000":// Handle invalid requestprint("OTPless Error: \(errorMessage ??"Unknown error")")// Internet-related errorscase"9100":print("OTPless Error: Request Timeout - \(errorMessage ??"The request took too long to complete and was aborted.")")case"9101":print("OTPless Error: Network Connection Was Lost - \(errorMessage ??"The connection was interrupted before the request could complete.")")case"9102":print("OTPless Error: DNS Lookup Failed - \(errorMessage ??"The domain name could not be resolved, possibly due to network issues.")")case"9103":print("OTPless Error: Cannot Connect to Server - \(errorMessage ??"The server is unreachable, possibly due to downtime or incorrect configurations.")")case"9104":print("OTPless Error: No Internet Connection - \(errorMessage ??"The device is not connected to the internet.")")case"9105":print("OTPless Error: Secure Connection Failed - \(errorMessage ??"A secure connection could not be established due to SSL/TLS issues.")")case"9110":print("OTPless Error: Otpless Authentication Request Cancelled - \(errorMessage ??"The authentication request was manually canceled by the user or the system.")")default:print("OTPless Error: \(errorMessage ??"Unknown error")")}}
Initiate the authentication process based on the user’s selected method by using the initiate method of the SDK.
Phone Authentication 📱
Phone authentication allows users to verify their identity using their phone number. Merchants can choose from various authentication methods:
Silent Authentication (SNA) – Automatically verifies the user without requiring OTP or MAGICLINK.
OTP on Desired Channel – Sends a one-time password (OTP) via SMS, WhatsApp, or another preferred channel.
Magic Link – Sends a link that users can click to authenticate.
SNA + OTP – Uses silent authentication first and falls back to OTP if needed.
OTP + Magic Link – Sends both an OTP and a magic link, allowing users to authenticate via either method.
otplessTask?.cancel()// Cancel any ongoing tasks to prevent request duplicationlet request =OtplessRequest()request.set(phoneNumber:"9899XXXXXX", withCountryCode:"COUNTRY_CODE")otplessTask =Task(priority:.userInitiated){awaitOtpless.shared.start(withRequest: request)}
Verify OTP
To verify the OTP entered by the user, use the verify method with the necessary parameters. Verifying OTP is required only in case of OTP authentication. No need to verify OTP in case of MAGICLINK.
let request =OtplessRequest()request.set(phoneNumber:"9899XXXXXX", withCountryCode:"COUNTRY_CODE")request.set(otp: otp)Task(priority:.userInitiated){awaitOtpless.shared.start(withRequest: request)}