A complete user authentication REST API built with TypeScript, Express, and MongoDB, implementing JWT authentication, refresh tokens, email verification, and secure cookie-based auth.
- User registration with email verification
- Secure login with JWT-based authentication (access + refresh tokens)
- Refresh token rotation using HTTP-only cookies
- Runtime Input validation with Zod
- Forgot password flow with email-based reset link
- Protected routes with authentication middleware
- Cookie-based token storage
- MongoDB with Mongoose ODM
- Docker support for MongoDB
- Node.js (v18 or higher)
- Docker and Docker Compose
- pnpm/npm/yarn/bun
git clone https://github.com/sagarkemble/Authentication-service
cd Authentication-servicepnpm installCreate a .env file in the root directory and add the variables as shown in the .env.example file
Important: Replace the JWT secrets with strong strings in production.
Start the MongoDB container:
npm run db:upThis will start MongoDB on localhost:27017 with:
- Username:
admin - Password:
password - Database:
authdb(or whatever you specify in MONGODB_URI)
To stop the database:
npm run db:downnpm run devThe server will start on http://localhost:3000 (or the PORT you specified in .env).
All routes are prefixed with /auth
POST /auth/register
Content-Type: application/json
{
"email": "user@example.com",
"password": "yourpassword",
"name": "John Doe"
}POST /auth/verify-mail
Content-Type: application/json
{
"token": "verification_token_from_email"
}POST /auth/login
Content-Type: application/json
{
"email": "user@example.com",
"password": "yourpassword"
}POST /auth/refresh-access-token
Cookie: refreshToken=<refresh_token>GET /auth/getme
Cookie: accessToken=<access_token>PATCH /auth/change-avatar
Cookie: accessToken=<access_token>
Content-Type: multipart/form-dataRequest Body (form-data):
avatar(file)- Field name must be avatar
- Max file size: 2MB
- Allowed formats: image/png, image/jpeg
POST /auth/logout
Cookie: accessToken=<access_token>POST /auth/forgot-password
Content-Type: application/json
{
"email": "user@example.com"
}POST /auth/reset-password
Content-Type: application/json
{
"email": "user@example.com"
}npm run dev- Start development server with hot reloadnpm run db:up- Start MongoDB Docker containernpm run db:down- Stop MongoDB Docker containernpm run commit- Commit with Commitizen (conventional commits)
This project uses Husky for git hooks:
- Pre-commit: Runs linting/formatting checks
- Commit-msg: Validates commit message format (conventional commits)
