- You have learned the basics of Node.js and Express.js, now let's test your knowledge of how to implement authentication with this Task Management project using JWT tokens and bcrypt password hashing.
- Fork and Clone this project repository in your terminal
- CD into the project base directory
cd Week20_Task_Management_NodeJS_Auth - Install dependencies:
npm install
- Create a
.envfile in the root directory with your database URL and JWT secret - Complete the authentication middleware and routes (see tasks below)
- Generate Prisma client and push schema to database:
npm run db:generate npm run db:push
- Start the server:
npm run dev
- The server will run on
http://localhost:3000
You need to complete the following:
Location: middleware/auth.js
The middleware file needs to be implemented to verify JWT tokens:
Requirements:
- Extract JWT token from
Authorization: Bearer <token>header - Verify the token using the JWT_SECRET
- Find the user in the database using the decoded userId
- Set
req.userwith the user data (excluding password) - Handle token validation errors properly
Location: routes/auth.js - POST /register endpoint
The registration endpoint needs to be implemented:
Requirements:
- Validate that email, password, and name are provided
- Check if a user with the email already exists
- Hash the password using bcrypt (12 salt rounds)
- Create the user in the database
- Generate a JWT token with userId and email
- Return user data (excluding password) and token
- Handle duplicate email errors
Location: routes/auth.js - POST /login endpoint
The login endpoint needs to be implemented:
Requirements:
- Validate that email and password are provided
- Find the user by email in the database
- Check if the user exists
- Compare the provided password with the hashed password using bcrypt
- Generate a JWT token with userId and email
- Return user data (excluding password) and token
- Handle invalid credentials errors
Location: .env
Create a .env file with:
DATABASE_URL="your-supabase-database-connection-url"
JWT_SECRET="your-super-secret-jwt-key"
PORT=3000You can use Postman or curl to test your endpoints, make sure everything is working correctly.
Objective: Implement password reset with email verification.
Requirements:
- Create password reset endpoint that generates a reset token
- Send reset email with reset link (simulate with console.log)
- Create reset password endpoint that validates reset token
- Update user password in database
Objective: Implement email verification for new registrations.
Requirements:
- Add email verification field to User model
- Generate verification token during registration
- Send verification email (simulate with console.log)
- Create verification endpoint to confirm email
- Update user verification status
Good luck with your implementation! 🚀