A one-time password (OTP) is an automatically generated string sent to the user during the
onboarding (sign-up or sign-in) process to authenticate that user. The OTP can be sent to an email
address or a mobile phone as a text. A typical method for implementing OTP has two
sets of functionality you need to program: user interaction and session verification.
Sign-Up or Sign-In (Signs up a new user or signs in an existing user): The following actions must be completed, first User Sign-Up or Sign-In then User Verification
For registering a new user, your application should accept user information, including an email or
phone number used for verification. In this sample code, the OTP verification will be sent by email
to email@company.com. To change the delivery method to send the OTP verification as a Text Message (SMS), you would
change the deliveryMethod to sms within the below example.
Note that signup is not complete without the user verification step below.
For authenticating a user, your application should accept the user's identity (typically an email address or phone
number). In this sample code, the OTP verification will be sent by email to email@company.com.
Note that signin is not complete without the user verification step below.
For signing up a new user or signing in an existing user, you can utilize the signUpOrIn functionality.
Only user loginId is necessary for this function. In this sample code, the OTP verification will be
sent by email to email@company.com. To change the delivery method to send the OTP verification as a Text Message (SMS), you would
change the deliveryMethod to sms within the below example.
Note that signUpOrIn is not complete without the user verification step below.
The next step in authenticating the user is to verify the code entered by the user, using OTP verify code
function. The function will return all the necessary JWT tokens,
claims and user information. You can use the JWT tokens for session validation in your application middleware or app
server for every route needs an authenticated user.
The Descope SDK allows for you to update user's email address. With this function, you will pass the user's loginId and the new email
address you want associated to the user. In order to verify the email address, the OTP code will be sent via the email delivery
method. Once the update email function has been called, the user will need to verify with the sent OTP code before the email
address will be updated.
The Descope SDK allows for you to update user's phone number. With this function, you will pass the user's loginId and the new
phone number you want associated to the user. In order to verify the phone number, the OTP code will be sent via the sms delivery
method. Once the update phone function has been called, the user will need to verify with the sent OTP code before the phone
number will be updated.
The final step of completing the authentication with Descope is to validate the user session. Descope provides rich session management capabilities, including configurable session timeouts and
logout functions. You can find the details and sample code for client session validation here.
Checkpoint
Your application is now integrated with Descope. Please test with sign-up or sign-in use case.