An open-source authentication backend built with Node.js, Express.js, MongoDB, and TypeScript.
It includes JWT-based authentication, refresh tokens with database sessions, role-based access control, rate limiting, and password management. Designed for real-world projects and easily extendable.
- β User registration & login with hashed passwords
- β JWT authentication (access + refresh tokens)
- β Database-backed refresh tokens (sessions stored in MongoDB)
- β
Role-based authorization (
user&admin) - β Password change & session invalidation
- β Logout & logout-all devices
- β Rate limiting per IP (prevents brute force login)
- β
Error handling with standardized
ApiError - β Environment configuration with Zod validation
- β Fully typed with TypeScript (strict mode)
- β RESTful API design
- Node.js (v18+)
- Express.js
- MongoDB (local or Atlas)
- Mongoose
- TypeScript (strict mode)
- Zod for environment & request validation
- JWT for authentication
- Bcrypt for password hashing
- dotenv for environment variables
- ts-node / nodemon for development
src/
ββ config/
β ββ db.ts
β ββ env.ts
ββ controllers/
β ββ logout.controller.ts
β ββ refresh.controller.ts
β ββ session.controller.ts
β ββ user.controller.ts
ββ middlewares/
β ββ auth.middleware.ts
β ββ error.middleware.ts
β ββ rateLimit.middleware.ts
β ββ role.middleware.ts
β ββ validate.middleware.ts
ββ models/
β ββ Session.model.ts
β ββ User.model.ts
ββ routes/
β ββ admin.routes.ts
β ββ index.ts
β ββ logout.routes.ts
β ββ refresh.routes.ts
β ββ session.routes.ts
β ββ user.routes.ts
ββ services/
β ββ refresh.service.ts
β ββ session.service.ts
β ββ user.service.ts
ββ types/
β ββ express.d.ts
ββ utils/
β ββ ApiError.ts
β ββ jwt.ts
ββ validators/
β ββ session.validator.ts
β ββ user.validator.ts
ββ app.ts
ββ server.ts1οΈβ£ Clone the repository
git clone https://github.com/hamidukarimi/authforge-express.git
cd authforge-express2οΈβ£ Install dependencies
npm install3οΈβ£ Create .env file
You must create a .env file in the root of the project.
You can copy from .env.example:
cp .env.example .envOr manually create one with:
PORT=5000
NODE_ENV=development
MONGO_URI=your_mongodb_uri
JWT_ACCESS_SECRET=your_access_secret
JWT_REFRESH_SECRET=your_refresh_secret
JWT_ACCESS_EXPIRES_IN=10m
JWT_REFRESH_EXPIRES_IN=7d
CLIENT_URL=http://localhost:3000All environment variables are validated at startup using Zod. The server will not start if any required variable is missing or invalid.
Development mode (with nodemon + ts-node)
npm run devBuild
npm run buildProduction mode
npm startBy default, the server runs on:
http://localhost:5000| Route | Method | Auth Required | Description |
|---|---|---|---|
/api/users/ |
POST | β | Register a new user |
/api/sessions/ |
POST | β | Login user (returns access & refresh tokens) |
/api/logout/ |
POST | β | Logout current session |
/api/logout/all |
POST | β | Logout all sessions |
/api/token/ |
POST | β | Get new access token using refresh token |
/api/users/me/password |
PUT | β | Change password |
Authorization: Bearer <access_token>
Content-Type: application/jsonRequest
POST /api/users/
Content-Type: application/json{
"name": "John Doe",
"email": "user@example.com",
"password": "password123"
}Response
{
"success": true,
"message": "User created successfully",
"data": {
"user": {
"id": "64f1a2b3c4d5e6f7g8h9",
"name": "John Doe",
"email": "user@example.com",
"role": "user"
},
"accessToken": "eyJhbGciOiJIUzI1NiIsIn..."
}
}Request
POST /api/sessions/
Content-Type: application/json{
"email": "user@example.com",
"password": "password123"
}Response
{
"success": true,
"message": "Logged in successfully",
"data": {
"user": {
"id": "64f1a2b3c4d5e6f7g8h9",
"name": "John Doe",
"email": "user@example.com",
"role": "user"
},
"accessToken": "eyJhbGciOiJIUzI1NiIsIn..."
}
}The refresh token is stored in an httpOnly cookie automatically.
This project uses strict TypeScript with the following key patterns:
- Mongoose models are fully typed using
IDocument,IMethods, andModelgenerics - Express
Requestis extended viasrc/types/express.d.tsto includereq.user - JWT payloads are typed with
AccessTokenPayloadandRefreshTokenPayloadinterfaces - Zod schemas export inferred types (
LoginInput,RegisterInput, etc.) as the single source of truth for request shapes - All
catchblocks narrowunknownerrors safely withinstanceofchecks
This project is open-source.
You are welcome to fork, submit pull requests, or open issues.
git checkout -b feature/my-feature
git commit -m "feat: add my feature"
git push origin feature/my-featureThen open a Pull Request.
This project is licensed under the MIT License.
See the LICENSE file for details.
- Easily extensible with email verification, password reset, or OAuth providers
- Ensure environment variables are properly configured before deployment
- Never commit JWT secrets to public repositories
If you find this project useful, consider giving it a star β on GitHub.
Made with β€οΈ by Hamid Karimi