This is the backend server for a MERN stack authentication system, offering secure APIs for user registration, login, and authentication. Built with Node.js, Express, and MongoDB, it ensures robust user management with JWT-based authentication and password hashing for enhanced security.
- User registration
- User login
- Token-based authentication
- Password hashing
- User logout
- Node.js
- Express.js
- MongoDB
- Mongoose
- JSON Web Tokens (JWT)
- bcrypt.js
- Node.js installed
- MongoDB installed and running
- Clone the repository:
git clone https://github.com/yourusername/Authentication-Backend.git
- Navigate to the server directory:
cd Authentication-Backend
- Install dependencies:
npm install
-
Create a
.env
file in the root of the server directory and add the following environment variables:PORT=5000 MONGO_URI=your_mongodb_connection_string JWT_SECRET=your_jwt_secret EMAIL_SERVICE=your_email_service_provider EMAIL_USERNAME=your_email_username EMAIL_PASSWORD=your_email_password
-
Refer to the
.env
file in your code to access these variables:require('dotenv').config(); const port = process.env.PORT; const mongoUri = process.env.MONGO_URI; const jwtSecret = process.env.JWT_SECRET; const emailService = process.env.EMAIL_SERVICE; const emailUsername = process.env.EMAIL_USERNAME; const emailPassword = process.env.EMAIL_PASSWORD;
Start the server:
npm start
The server will be running on http://localhost:5000
.
POST /api/auth/register
- Register a new userPOST /api/auth/login
- Login a userGET /api/auth/user
- Get authenticated user detailsPOST /api/auth/register
- Register a new userPOST /api/auth/login
- Login a userGET /api/auth/user
- Get authenticated user detailsPOST /api/auth/logout
- Logout a userPOST /api/auth/refresh-token
- Refresh authentication token
This project is licensed under the MIT License.