Testing our Serverless Authentication API

  • Make a POST request on /register route with the following JSON:
{
"name": "Arindam Halder",
"username": "arindam369",
"password": "abc#123"
}



POST on /register route

User Registration Response

  • Make a POST request on /login route with the following JSON:
{
"username": "arindam369",
"password": "abc#123"
}



POST on /login route

User Login Response

  • Make a POST request on /verify route with the following JSON:
{
"username": "arindam369",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFyaW5kYW0zNjkiLCJpYXQiOjE2OTgzMDg3OTksImV4cCI6MTY5ODMxMjM5OX0.Bmn90tlaFFr0Hmh0jXbtPPzlaWuEItMb61JaPM7cT-8"
}

POST on /verify route

User Verification Response

  • Make a POST request on /logout route with the following JSON:
{
"username": "arindam369",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFyaW5kYW0zNjkiLCJpYXQiOjE2OTgzMDg3OTksImV4cCI6MTY5ODMxMjM5OX0.Bmn90tlaFFr0Hmh0jXbtPPzlaWuEItMb61JaPM7cT-8"
}

POST on /logout route

User Logout Response

DynamoDB table data JSON view (after testing the operations)

AWS Lambda and Amazon DynamoDB for Serverless Authentication

Serverless Authentication is a method of authenticating users in a cloud-based application without the need for traditional server management. It leverages services like AWS Lambda and Amazon DynamoDB to handle user authentication processes in a cost-effective and efficient manner. By using these serverless components, developers can create secure and responsive authentication systems that scale with the application’s demands. AWS Lambda is a powerful cloud computing service provided by AWS that allows you to run code without provisioning or managing servers. It operates on an event-driven model, meaning it executes functions in response to specific events or triggers, optimizing resource usage and cost efficiency. Amazon DynamoDB, on the other hand, is a highly scalable NoSQL database service also provided by AWS. It’s designed for applications that require fast and predictable performance at any scale. DynamoDB offers seamless scaling of read and write capacity to handle variable workloads, ensuring rapid and reliable data storage.

In this article, we will learn how to create a Serverless Authentication using AWS Lambda and DynamoDB.

Similar Reads

How Authentication works?

When a user attempts to login into their account, the system prompts them to provide their credentials, which typically includes the username and password. The backend system then undertakes a critical verification process to ascertain the validity of these inputs. If, the provided credentials do not match any existing records, the system promptly communicates this to the user with a clear and concise message: “Wrong username or password”. If the provided credentials are correct, a string is generated i.e. called token which basically encapsulates the username of the user and gets stored in 2 places – a) in browser’s cookies and b) user’s tokens array in database. At the time of verification, whether the user is authenticated or not, we checks first, whether there is any specific token found in browser’s cookies or not. If found, then our backend service breaks the token and find the username encapsulated inside it. Now, it finds the user with that username in database. If no record found, then the user is not authenticated. Otherwise, it checks whether in the founded user’s tokens array, our token is present or not. If it is there, then the user is authenticated, otherwise not....

Create a DynamoDB Table

Login to your AWS account and go to DynamoDB service. Click upon the “Create Table” button to create a table “login-database”. For partition key, enter “username”. DynamoDB also offers a new option regarding the provisioning of read & write capacities: On-demand mode. Select the Table Settings as Customised Settings and then in Read Write capacity settings, select the capacity mode “On-demand” This mode is great if you have no idea about how much read and write capacity you will need. And you can of course always switch back to fixed limits, too. If you do know which limits make most sense for you, the traditional “in-advance” provisioning will be cheaper. Click on the “Create Table” button at the end. Now, you can see your created table in the Tables tab....

Create a Lambda Function

Now, we will create a lambda function for the backend of our API. The lambda function needs to handle the operations for sign up, sign in, sign out of users....

Create an API using API Gateway

Now, we will create our Login-API. We will use the API Gateway service. Let’s understand what we are gonna create, what will happen behind the scenes of the API —...

Setup Lambda Function

...

Testing our Serverless Authentication API

Previously, we created the Lambda function, now we have to handle the operations required in serverless authentication. Follow the steps below:...

FAQs On AWS Lambda and Amazon DynamoDB for Serverless Authentication :

...