Lab 1
Basic Login System
Scenario
The user portal for “Innovate Inc.” needs its core authentication feature. Before you can build out more complex functionality, you need to create the fundamental endpoints for user registration and login. Your task is to build a simple Express API that can:
- Accept a new user’s credentials and create a user record with a hashed password.
- Accept a returning user’s credentials, validate them against the stored hash, and issue a JWT upon success.
Instructions
Task 1: Project Setup
- Create a new project directory and initialize it with
npm. - Install the necessary packages:
express,mongoose,bcrypt,jsonwebtoken, anddotenv. - Set up your
server.jsfile, an Express router for your user-related endpoints, and aUsermodel based on the schema from Lesson 1. - Establish a connection to your MongoDB database. Use a
.envfile to store yourMONGO_URI, aJWT_SECRET, and aPORT.
Task 2: Build the Registration Endpoint
- Create a
POSTroute (e.g.,/api/users/register). - In this route, take the
username,email, andpasswordfrom thereq.body. - Check if a user with the given email already exists. If so, return a
400status with an appropriate message. - If the user does not exist, create a new
Userdocument with the provided data. Thepre-savehook you wrote in Lesson 1 should automatically hash the password. - Save the new user to the database.
- Respond with a
201status and the newly created user object (excluding the password).
Task 3: Build the Login Endpoint
- Create a
POSTroute (e.g.,/api/users/login). - In this route, take the
emailandpasswordfrom thereq.body. - Find a user in the database with the matching email. If no user is found, return a
400status with a generic error message (e.g., “Incorrect email or password.”). - If a user is found, use the
isCorrectPasswordinstance method (orbcrypt.compare) to compare the incoming password with the user’s stored hashed password. - If the passwords do not match, return a
400status with the same generic error message. - If the passwords match, create a JWT for the user using the
jsonwebtokenlibrary. The payload should contain non-sensitive user data like their_idandusername. - Respond with a JSON object containing the signed
tokenand the user’s data.
Acceptance Criteria
- Your Express server runs without errors.
- The
POST /api/users/registerendpoint successfully creates a new user with a hashed password and returns the user’s data. - The
POST /api/users/loginendpoint successfully validates a user’s credentials and returns a signed JWT. - The login endpoint correctly rejects incorrect passwords or non-existent users.
Submission
- Submit a link to your completed GitHub repository.
- Ensure your
.envfile andnode_modulesdirectory are included in your.gitignorefile and are not pushed to the repository.
Grading
This is a complete/incomplete assignment. As long as your submission meets all the acceptance criteria, you will receive full credit.