This project demonstrates how to implement session-based authentication in a NestJS application using MongoDB as the database. It covers user creation, authentication, and route protection.
- Node.js installed on your system.
- MongoDB installed and running.
- Install Dependencies: Run
npm install
to install dependencies. - Set Up MongoDB Database.
- Configure the database connection in
app.module.ts
using the MongooseModule.forRoot() method. - Run
npm run start:dev
to start the server.
- User Registration: Allows users to sign up with a username and password.
- User Authentication: Users can log in with their credentials.
- Session Management: Utilizes express-session for session management.
- Route Protection: Protects routes to ensure only authenticated users can access them.
- Sign Up: Send a POST request to /users/signup with a username and password.
- Log In: Send a POST request to /users/login with the same credentials.
- Access Protected Route: After logging in, you can access protected routes.
Session storage is saved by default in 'MemoryStore,' which is not intended for production use. For production environments, consider using a data store like Redis for stability and performance.
- NestJS Documentation
- MongoDB Documentation
- Passport.js Documentation
This project is licensed under the MIT License.