Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SecureAuth MERN

A full-stack authentication platform built with the MERN stack (MongoDB, Express.js, React, and Node.js). The application provides a secure and modern authentication workflow, including account registration, email verification, password recovery, JWT-based authentication, and protected routes.

Features

Authentication

  • User registration
  • User login and logout
  • JWT authentication
  • Secure password hashing with bcrypt
  • Protected API routes
  • Authentication state management

Email Verification

  • Verification code generation
  • Email account confirmation
  • Verification token expiration handling
  • Welcome email after successful verification

Password Recovery

  • Forgot password functionality
  • Secure password reset tokens
  • Password reset expiration validation
  • Password reset confirmation email

Security

  • Password hashing using bcrypt
  • JWT token-based authentication
  • HTTP-only cookie support
  • Secure token generation
  • Input validation
  • Environment variable configuration

User Management

  • User profile retrieval
  • Authentication status checking
  • Last login tracking
  • Account verification status

Tech Stack

Frontend

  • React
  • Vite
  • JavaScript
  • Axios
  • React Router

Backend

  • Node.js
  • Express.js
  • MongoDB
  • Mongoose
  • JWT
  • bcryptjs
  • Cookie Parser

Email Service

  • Mailtrap

Project Structure

SecureAuth-MERN/
│
├── backend/
│   ├── controllers/
│   ├── middleware/
│   ├── models/
│   ├── routes/
│   ├── mailtrap/
│   ├── utils/
│   └── server.js
│
├── frontend/
│   ├── public/
│   ├── src/
│   │   ├── components/
│   │   ├── pages/
│   │   ├── store/
│   │   └── utils/
│   └── vite.config.js
│
├── .env
├── package.json
└── README.md

Environment Variables

Create a .env file inside the backend directory and configure the following variables:

PORT=5000

MONGO_URI=your_mongodb_connection_string

JWT_SECRET=your_jwt_secret_key

CLIENT_URL=http://localhost:5173

MAILTRAP_TOKEN=your_mailtrap_token
MAILTRAP_ENDPOINT=your_mailtrap_endpoint

NODE_ENV=development

Installation

Clone the repository

git clone https://github.com/Pa004/SecureAuth-MERN.git
cd SecureAuth-MERN

Install dependencies

npm install

Install frontend dependencies:

cd frontend
npm install

Running the Application

Development Mode

Start the backend server:

npm run dev

Start the frontend application:

cd frontend
npm run dev

Production Build

Build the frontend:

cd frontend
npm run build

Run the application:

npm start

Authentication Workflow

  1. User creates an account.
  2. A verification code is sent via email.
  3. User verifies the account.
  4. User can log in securely.
  5. JWT token is generated and stored.
  6. Protected routes become accessible.
  7. Password recovery is available if needed.

API Endpoints

Authentication

Method Endpoint Description
POST /api/auth/signup Register a new user
POST /api/auth/login Authenticate user
POST /api/auth/logout Logout user
POST /api/auth/verify-email Verify email address
POST /api/auth/forgot-password Request password reset
POST /api/auth/reset-password/:token Reset password
GET /api/auth/check-auth Check authentication status

Security Practices

  • Passwords are never stored in plain text.
  • Authentication tokens are securely generated.
  • Password reset links have expiration times.
  • Email verification codes expire automatically.
  • Sensitive information is stored in environment variables.
  • Protected routes require valid authentication.

Future Enhancements

  • Role-based access control (RBAC)
  • Multi-factor authentication (MFA)
  • OAuth integration (Google, GitHub)
  • Refresh token support
  • User profile management
  • Automated testing
  • Docker deployment
  • CI/CD pipeline integration

License

This project is intended for educational and development purposes.

About

Secure MERN authentication system with JWT, email verification, password recovery, protected routes, and MongoDB integration.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages