This project is a sample application built with Node.js and MongoDB, demonstrating a simple authentication system. It provides a foundation for creating authentication services using JWT (JSON Web Tokens) with secure routes.
- User registration with password hashing
- User login with JWT-based authentication
- MongoDB integration for storing user data
- Basic middleware for verifying JWTs
-
Clone this repository:
git clone https://github.com/AmirDanesh/auth-nodejs-mongo-sample.git
-
Navigate to the project directory:
cd auth-nodejs-mongo-sample -
Install dependencies:
npm install
-
Set up your MongoDB database and add your connection string to the
.envfile:MONGO_URI=mongodb://localhost:27017/your_database JWT_SECRET=your_jwt_secret
-
Run the application:
npm start
Registers a new user.
- Request Body:
{ "username": "user1", "password": "password123" }
Logs in a user and returns a JWT.
- Request Body:
{ "username": "user1", "password": "password123" }
Fetches the profile of the currently authenticated user.
- Authorization: Bearer token in the request header.
- Node.js
- Express
- MongoDB
- JWT Authentication
This project is licensed under the MIT License - see the LICENSE file for details.