This project is a fully functional authentication REST API built with Node.js, TypeScript, and Typegoose. It offers user registration, email verification, password recovery, and JWT-based authentication (access and refresh tokens).
- User Registration - Create a new account with an email and password.
- Email Verification - Verify a user's email after registration.
- Forgot Password - Send a password reset email.
- Password Reset - Reset a user's password.
- Get Current User - Retrieve details about the authenticated user.
- Login - Authenticate with email and password.
- Access Token - Obtain a short-lived access token.
- Refresh Token - Obtain a long-lived refresh token to renew the access token.
- TypeScript - Static type checking for enhanced developer experience.
- Express - A flexible and lightweight web server framework for building RESTful APIs.
- Typegoose - A Mongoose wrapper that simplifies TypeScript integration for MongoDB schemas and models.
- argon2 - Secure password hashing algorithm.
- Zod - Data validation and parsing, ensuring strict API schema adherence.
- jsonwebtoken - Generating and verifying JSON Web Tokens (JWT) for authentication.
- Nodemailer - Sending email notifications for actions like email verification and password resets.
- Pino - High-performance logging for better debugging and monitoring.
- Node.js (>=14.x.x)
- MongoDB - The database used for storing user data and tokens.
-
Clone the Repository
git clone https://github.com/ramthenmala/auth-api.git cd authentication-rest-api
-
Install Dependencies
pnpm install
-
Environment Variables
Create a
.env
file in the root directory with the following:PORT=4000 MONGODB_URI=mongodb://localhost:27017/auth-db JWT_SECRET=your_jwt_secret JWT_ACCESS_TOKEN_EXPIRES_IN=15m JWT_REFRESH_TOKEN_EXPIRES_IN=7d EMAIL_HOST=smtp.your-email-provider.com EMAIL_PORT=587 [email protected] EMAIL_PASS=your-email-password
-
Start the Server
pnpm run dev
The API will be available at
http://localhost:4000
.
Endpoint: GET /healthcheck
curl -X GET http://localhost:4000/healthcheck
Endpoint: POST /api/users
curl -X POST http://localhost:4000/api/users \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "Password123!"
}'
Endpoint: POST /api/users/verify/:id/:verificationCode
Replace :id
and :verificationCode
with the actual user ID and verification code.
curl -X POST http://localhost:4000/api/users/verify/USER_ID/VERIFICATION_CODE
Endpoint: POST /api/users/forgotpassword
curl -X POST http://localhost:4000/api/users/forgotpassword \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]"
}'
Endpoint: POST /api/users/resetpassword/:id/:passwordResetCode
Replace :id
and :passwordResetCode
with the actual user ID and password reset code.
curl -X POST http://localhost:4000/api/users/resetpassword/USER_ID/PASSWORD_RESET_CODE \
-H "Content-Type: application/json" \
-d '{
"password": "NewPassword123!"
}'
Endpoint: POST /api/sessions
curl -X POST http://localhost:4000/api/sessions \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "Password123!"
}'
Endpoint: GET /api/users/me
Requires authentication. Replace ACCESS_TOKEN
with a valid JWT access token.
curl -X GET http://localhost:4000/api/users/me \
-H "Authorization: Bearer ACCESS_TOKEN"
Endpoint: POST /api/sessions/refresh
Requires authentication. Replace REFRESH_TOKEN
with a valid JWT refresh token.
curl -X POST http://localhost:4000/api/sessions/refresh \
-H "Authorization: Bearer REFRESH_TOKEN"