This repository provides a comprehensive implementation of user authentication using JSON Web Tokens (JWT) in a MERN (MongoDB, Express, React, Node.js) stack application. The project is designed to guide developers in creating secure, token-based authentication systems for their web applications. It encompasses user registration, login, protected routes, and token management, demonstrating best practices in web security.
- User Registration: Users can create an account by providing necessary details, which are validated and stored in a MongoDB database.
- User Login: Secure login mechanism that generates JWT tokens upon successful authentication.
- Token Management: Implementation of JWT for secure communication between client and server, including token expiration and refresh.
- Protected Routes: Middleware to protect certain routes and ensure that only authenticated users can access them.
- Error Handling: Comprehensive error handling to provide meaningful feedback during authentication processes.
- Environment Configuration: Easy configuration for different environments using environment variables.
- Frontend: React
- Backend: Node.js with Express
- Database: MongoDB
- Authentication: JSON Web Tokens (JWT)
- Middleware: Express middleware for route protection
- Development Tools: npm, Postman for API testing
-
Clone the Repository:
git clone https://github.com/yourusername/Complete-Authentication-with-JWT.git cd Complete-Authentication-with-JWT -
Install Dependencies:
- Navigate to both the client and server directories and install the necessary packages:
cd client npm install cd ../server npm install
-
Set Up Environment Variables:
- Create a
.envfile in the server directory and add the following variables:
PORT=5000 MONGO_URI=your_mongo_database_uri JWT_SECRET=your_jwt_secret_key
- Create a
-
Run the Application:
- Start the server:
cd server npm start- Start the client:
cd client npm start
- Register: Send a POST request to
/api/auth/registerwith the user details. - Login: Send a POST request to
/api/auth/loginwith credentials to receive a JWT. - Access Protected Routes: Include the JWT in the
Authorizationheader as a Bearer token when accessing protected routes.
The API endpoints are structured as follows:
- POST /api/auth/register: Register a new user
- POST /api/auth/login: Authenticate user and return JWT
- GET /api/protected: Example protected route requiring a valid JWT
Contributions are welcome! If you have suggestions or improvements, please fork the repository and create a pull request.