This repository implements full JWT-based authentication using Node.js, Express, and TypeScript. It provides features for user registration, activation, login, access token refresh, password reset, and more. Authentication and route protection are handled through JSON Web Tokens (JWT), offering a secure and scalable solution.
- User Registration with Email Activation
- Login and Logout
- JWT-based Access Token and Refresh Token Mechanism
- Password Reset via Email
- User Info and Profile Update
- Secure Routes with JWT Authentication Middleware
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /registration |
Registers a new user | No |
POST | /activate-user |
Activates a newly registered user | No |
POST | /login |
Logs in a user | No |
GET | /logout |
Logs out a user | Yes |
GET | /refresh |
Refreshes the access token | No |
GET | /me |
Fetches user information | Yes |
PUT | /update-user-info |
Updates the userβs information | Yes |
PUT | /change-password |
Changes the userβs password | Yes |
POST | /reset-password-link |
Sends a password reset link to the user | No |
POST | /reset-password/:id/:token |
Resets the userβs password using token | No |
This project was developed using the following technologies:
Ensure you have the following installed on your machine:
- Node.js 18.x
- npm or yarn
-
Clone the repository:
git clone https://github.com/deepak-raaaz/express-jwt-auth.git cd jwt-auth
-
Install the dependencies:
npm install
-
Create a
.env
file in the root directory and add the following:PORT=8080 ORIGIN=['http://localhost:8080/'] NODE_ENV=development DB_URL=your_db_url CLOUD_NAME=your_cloud_name CLOUD_API_KEY=your_cloud_api_key CLOUD_SECRET_KEY=your_cloud_secret_key REDIS_URL=your_redis_url ACTIVATION_SECRET=your_activation_secret_code ACCESS_TOKEN=your_access_token REFRESH_TOKEN=your_refresh_token ACCESS_TOKEN_EXPIRE=5 REFRESH_TOKEN_EXPIRE=60 SMTP_HOST=your_smtp_host SMTP_PORT=your_smtp_port SMTP_USER=your_smtp_user SMTP_PASS=your_smtp_password
-
Build the project:
npm run build
-
Start the server:
npm run start
You can run the project in development mode using the following command:
npm run dev
This will use ts-node-dev
for live reload and faster development.
Use tools like Postman or cURL to test the API endpoints. Make sure to provide valid JWT tokens for protected routes.
POST /login
Content-Type: application/json
{
"email": "[email protected]",
"password": "password123"
}
GET /me
Authorization: Bearer <access-token>
Contributions are welcome! Here's how you can help:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/AmazingFeature
). - Commit your changes (
git commit -m 'Add some AmazingFeature'
). - Push to the branch (
git push origin feature/AmazingFeature
). - Open a pull request.
Distributed under the ISC License. See LICENSE
for more information.
Deepak Kumar - [email protected]