Serverless authentication backend using AWS Lambda, DynamoDB, and JWT.
https://github.com/davidbmar/login-system · public · shipped
A template login system implemented as an AWS Lambda function. It handles user registration, login, and token verification. User credentials are stored in a DynamoDB table with passwords hashed via bcrypt. Authentication state is managed using JSON Web Tokens (JWT).
flowchart TD
Client[Client App] -->|HTTP POST/GET| APIGW[API Gateway]
APIGW -->|Event| Lambda[AWS Lambda Handler]
Lambda -->|Read/Write| DynamoDB[(DynamoDB Table)]
Lambda -->|Sign/Verify| JWT[JSON Web Token]
Lambda -->|Hash/Compare| Bcrypt[bcryptjs]
subgraph AWS Cloud
Lambda
DynamoDB
end
The core logic resides in a single Lambda handler (`index.js`) that routes HTTP requests to service modules for registration, login, and verification. It uses the `aws-sdk` for DynamoDB interactions, `bcryptjs` for password hashing, and `jsonwebtoken` for token generation and validation. Responses are formatted as API Gateway-compatible JSON objects.
sequenceDiagram
participant C as Client
participant L as Lambda Handler
participant S as Service Layer
participant D as DynamoDB
participant A as Auth Utils
C->>L: POST /register or /login
L->>S: Call register() or login()
alt Registration
S->>D: Get User (check existence)
D-->>S: User Data
S->>S: Hash Password (bcrypt)
S->>D: Save User
else Login
S->>D: Get User
D-->>S: User Data
S->>S: Compare Password (bcrypt)
S->>A: Generate JWT
A-->>S: Token
end
S-->>L: Response Object
L-->>C: JSON Response
Deploy this code as an AWS Lambda function behind an API Gateway. Ensure the `audio_client_server_users` DynamoDB table exists in `us-east-2`. Set the `JWT_SECRET` environment variable. Use the exposed endpoints for user management in web or mobile applications.