A TypeScript-based Authentication Service using JWT tokens with access and refresh token support.
- User registration and login
- JWT-based authentication with access and refresh tokens
- Token validation and refresh mechanisms
- User profile management
- Swagger API documentation
- MongoDB integration
- Error handling and logging
- TypeScript support
- Node.js (v14 or higher)
- MongoDB
- TypeScript
-
Clone the repository:
git clone https://github.com/Krishna7852/node-typescript-cas-api.git cd node-typescript-cas-api -
Install dependencies:
npm install
-
Create a
.envfile in the root directory with the following variables:PORT=3000 MONGODB_URI=mongodb://localhost:27017/cas_service JWT_ACCESS_SECRET=your_access_secret_key_here JWT_REFRESH_SECRET=your_refresh_secret_key_here ACCESS_TOKEN_EXPIRES_IN=15m REFRESH_TOKEN_EXPIRES_IN=7d NODE_ENV=development
Run the development server:
npm run devBuild the project:
npm run buildStart the production server:
npm startOnce the server is running, you can access the Swagger API documentation at:
http://localhost:3000/api-docs
- Description: Logs in a user and returns a token.
- Request Body:
{ "username": "string", "password": "string" }
- Description: Registers a new user.
- Request Body:
{ "username": "string", "password": "string", "email": "string" }
- Description: Validate access token
- Description: Get user details
- Description: Retrieves a list of users.
- Description: Retrieves a specific user by ID.
- Description: Updates a user by ID.
- Request Body:
{ "username": "string", "email": "string" }
- Description: Deletes a user by ID.
- Description: Uploads a file.
- Description: Retrieves a file by ID.
- Description: Deletes a file by ID.
| Variable | Description | Default |
|---|---|---|
| PORT | Server port | 3000 |
| MONGODB_URI | MongoDB connection string | mongodb://localhost:27017/cas_service |
| JWT_ACCESS_SECRET | JWT access token secret | - |
| JWT_REFRESH_SECRET | JWT refresh token secret | - |
| ACCESS_TOKEN_EXPIRES_IN | Access token expiry | 15m |
| REFRESH_TOKEN_EXPIRES_IN | Refresh token expiry | 7d |
| NODE_ENV | Environment | development |
- Passwords are hashed using bcrypt
- JWT tokens for secure authentication
- Access and refresh token mechanism
- Protected routes using authentication middleware
The API uses a centralized error handling mechanism with appropriate HTTP status codes and error messages.
-
Fork the repository
-
Create your feature branch (
git checkout -b feature/amazing-feature) -
Commit your changes (
git commit -m 'Add some amazing feature') -
Push to the branch (
git push origin feature/amazing-feature) -
Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details
