This repository contains a full-stack MERN (MongoDB, Express, React, Node.js) application for implementing user authentication using JSON Web Tokens (JWT). It includes essential features such as registration, login, password reset, and password update, designed to handle secure user authentication in modern web applications.
- User Registration: Allows new users to sign up by providing their email, password, and basic details. Passwords are securely hashed using bcrypt before storing them in the database.
- User Login: Authenticated users can log in with their email and password. A JWT is generated and sent back as a response, which is used to authorize subsequent requests.
- Forgot Password: Users can request a password reset link via email. The link expires after a certain time, and users can set a new password securely.
- Update Password: Logged-in users can update their password. The new password is hashed and updated in the database.
- JWT Authentication: The application uses JWT to manage session tokens and secure communication between the client and server. Tokens are stored in HTTP-only cookies to prevent cross-site scripting (XSS) attacks.
- Password Hashing: All passwords are hashed using bcrypt to ensure user data security.
- Error Handling: The app provides detailed error messages for failed authentication attempts, invalid input, or unauthorized actions.
- Responsive UI: A clean, user-friendly interface built with React ensures a seamless experience on both desktop and mobile devices.
- Frontend: React.js, Axios, CSS
- Backend: Node.js, Express.js
- Database: MongoDB
- Authentication: JSON Web Tokens (JWT), bcrypt for password hashing
- Email: NodeMailer (for sending password reset links)
- Node.js
- MongoDB (either locally or using MongoDB Atlas)
- A code editor (e.g., VSCode)
-
Clone the repository:
git clone https://github.com/yourusername/mern-jwt-authentication.git
-
Install dependencies for both frontend and backend:
-
Navigate to the
clientfolder and install frontend dependencies:cd client npm install -
Navigate to the
serverfolder and install backend dependencies:cd server npm install
-
-
Configure environment variables:
- In the
serverfolder, create a.envfile and add the following variables:MONGO_URI=your_mongo_db_connection_string JWT_SECRET=your_jwt_secret_key SMTP_HOST=smtp.your_email_service.com SMTP_PORT=your_email_service_port SMTP_USER=your_email_address SMTP_PASS=your_email_password
- In the
-
Start the application:
-
In the
serverfolder, start the backend server:npm start
-
In the
clientfolder, start the React frontend:npm start
-
Feel free to fork this repository and submit pull requests if you'd like to contribute to its development. Ensure that your code adheres to the project's coding standards and includes tests for new features.
This project is licensed under the MIT License - see the LICENSE file for details.
This project serves as a great starting point for adding user authentication to a MERN stack application with JWT, making it easier to handle secure login processes in modern web development.