Secure user Authentication is one of the challenging part in digital world, One Common way to address this challenge is One Time Password(OTP) verification process. In this blog post we will explore AWS SNS(Simple Notification Service) in Node js to Implement OTP verification Seamlessly. AWS SNS provides reliable and scalable infrastructure for sending SMS messages, Making it an ideal choice for otp delivery. So let’s dive in and learn how to Integrate OTP verification AWS SNS in Node js.
Before getting started, please ensure that you need to have these things.
A one time password (OTP) is an automatically or manually generated numeric or alphanumeric string of characters that authenticates a user for a single transaction or login session. An OTP is more secure than a static password, especially a user-created password, which can be weak and/or reused across multiple accounts.
To begin, First you need to have an AWS account, once you create the account Log in to your AWS console and navigate to the AWS SNS service and generate credentials for accessing AWS SNS. Click on the link provided below for redirection to AWS SNS service console.
I assumed that you have already installed node js and the respective web framework (whether it is express js or any other) for all node js web framework the process will be same, for using AWS SNS service we need to install aws sdk npm module and other required dependencies. The aws-sdk module provides the necessary tools to interact with AWS services.
Create a new file named .env in the project’s root directory. Add the following variables to it.
Replace the placeholders once you got this information from the AWS console.
First we need to initialize the aws-sdk module and it’s required configs
Here is the function for generate a random otp, using that random otp we prepare the payload with an message and phone number(Phone number should have country code), for send the generated otp to the user we need to call the aws-sdk publish function with that payload, after successfully runs the function a response returns that contains a messageId and requestId those will be used for otp verification, we have stored this information in database table(In our case UserOtp table) for future references.
Note :- userOtpsRepository and UserOtps denoted user otp table and the validation filtration domain object.
Here is the verification function, for verification of otp we need to retrieve the sent otp from user otp table by using session key that was generated at the time of otp generation, once we get the record, we calculate the difference of current time and the otp sent time if the difference is not equal to zero, it means this is the valid otp.
In this blog post we have discussed the process of implementing otp authentication using AWS SNS in Node js.
In Addition this otp verification process improves the security and scalability of applications.
Thanks for reading, I hope you have found this useful.