This project is a Node.js application that implements user authentication using JSON Web Tokens (JWT) with Passport.js and Express.
- User registration
- User login with JWT generation
- Route protection using JWT authentication
- Token storage in HTTP-only cookies
- User logout
- Node.js
- Express.js
- MongoDB (with Mongoose)
- Passport.js
- jsonwebtoken
- bcrypt
-
Clone the repository:
git clone https://github.com/your-username/your-project.git -
Install dependencies:
cd your-project npm install -
Create a
.envfile in the project root and add the following variables:JWT_SECRET=your_very_secure_secret MONGODB_URI=your_mongodb_uri PORT=8080
-
Start the server:
npm start -
The server will be running at
http://localhost:8080
POST /auth/register: Register a new userPOST /auth/login: Log in and return a JWTGET /profile: Protected route that requires authenticationGET /auth/logout: Log out the user
Passport configuration can be found in config/passport.js. It uses the JWT strategy to authenticate requests.
- JWT tokens are stored in HTTP-only cookies for enhanced security.
- Passwords are hashed before being stored in the database.
- Environment variables are used to store sensitive information.