Welcome to the Advanced MERN Authentication! This project provides a robust authentication system built with the MERN (MongoDB, Express, React, Node.js) stack. It includes essential features like user registration, login
, logout
, email
verification
, and password management
.
- π Check Authentication (
/check-auth
): Verify if the user is authenticated. - π Sign up (
/signup
): Register a new user account. - π Login (
/login
): Authenticate and log in a user. - πͺ Logout (
/logout
): Log out the user by invalidating the session/token. - π§ Email Verification (
/verify-email
): Verify a user's email address. - π Forgot Password (
/forgot-password
): Initiate the password reset process. - π Reset Password (
/reset-password/:token
): Reset the user's password using a secure token. - πͺ Generate Token and Set Cookie: Creates a JWT token and securely sets it in a cookie.
- π Frontend Pages: Includes frontend pages such as
Sign Up
,Login
,Dashboard
,Forgot Password
,Reset Password
, andEmail Verification
. - π¦ State Management: The project uses
Zustand
for state management to handle user authentication state. - β‘ API Integration:
Axios
is used for seamless integration with the authentication API. - π¨ UI Enhancements:
Tailwind CSS
is used for modern and responsive styling, whileFramer Motion
adds smooth animations and transitions for a dynamic user experience.
Clone the repository:
git clone https://github.com/mhdZhHan/advanced-mern-auth.git
cd advanced-mern-auth
npm run build
Create a .env
file in the root directory and add the following variables:
MONGODB_URL="" # MongoDB connection string
JWT_SECRET="" # Secret key for JWT signing
NODE_ENV="development" # Set to "production" in a live environment
MAILTRAP_TOKEN="" # Mailtrap API token for sending emails
MAILTRAP_ENDPOINT="" # Mailtrap API endpoint for email services
CLIENT_URL="http://localhost:5173" # URL of the React frontend application
npm run start
Your server should now be running on http://localhost:5000
.
The authentication-related API endpoints are available under the /api/auth
base URL. Here are the available routes:
- GET
/api/auth/check-auth
: Verifies if the user is authenticated by checking the token. - POST
/api/auth/signup
: Registers a new user and sends an email verification link. - POST
/api/auth/login
: Logs in the user and returns a JWT token. - POST
/api/auth/logout
: Logs out the user by clearing the session. - POST
/api/auth/verify-email
: Verifies the user's email address with a token. - POST
/api/auth/forgot-password
: Sends a password reset link to the user's email. - POST
/api/auth/reset-password/:token
: Resets the user's password using the provided token.
- MongoDB: Database for storing user information.
- Express: Web framework for handling routes and middleware.
- React: Frontend framework for creating the client-side application (not included in this repo).
- Node.js: Backend runtime environment.
- JSON Web Tokens (JWT): For secure user authentication.
- Mailtrap: Service for sending emails (used for email verification and password reset).
- Zustand: State management library for handling user authentication state.
- Axios: HTTP client for making API requests.
- Tailwind CSS: Utility-first CSS framework for modern and responsive styling.
- Framer Motion: Library for adding smooth animations and transitions for enhanced UI interactions.