Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad2769f

Browse files
allhandsondeckstasel
authored andcommitted
doc: add prep exercise of week 3
1 parent 58bb995 commit ad2769f

File tree

6 files changed

+49
-153
lines changed

6 files changed

+49
-153
lines changed

week3/build-with-students/app.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

week3/build-with-students/users.js

Lines changed: 0 additions & 134 deletions
This file was deleted.

week3/prep-exercise/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
In this exercise, you will build a secure authentication and authorisation system using Node.js and Express.js with four main endpoints: register, login, getProfile, and logout. The system will utilise JWT (JSON Web Tokens) for managing user sessions.
2+
3+
Requirements:
4+
5+
1. Register Endpoint:
6+
7+
- Implement a POST endpoint /register that allows users to register with a username and password.
8+
- Validate the request body to ensure it includes a username and password.
9+
- Hash the user's password using bcrypt before storing it in memory.
10+
- Return a success message along with the user's ID and username upon successful registration.
11+
12+
1. Login Endpoint:
13+
14+
- Create a POST endpoint /login that allows users to log in with their registered credentials.
15+
- Validate the request body to ensure it includes a username and password.
16+
- Verify the user's credentials by comparing the hashed password stored in memory.
17+
- If authentication succeeds, generate a JWT containing the user's ID and sign it with a secret key.
18+
- Return the JWT token to the client upon successful login.
19+
20+
1. Get Profile Endpoint:
21+
22+
- Implement a GET endpoint /profile that allows authenticated users to retrieve their profile information.
23+
- Extract the JWT token from the Authorization header.
24+
- Verify the JWT token and decode the payload to retrieve the user's ID.
25+
- Retrieve the user's profile information from memory using the decoded user ID.
26+
- Return a message with the user's username upon successful profile retrieval.
27+
28+
1. Logout Endpoint:
29+
30+
- Create a POST endpoint /logout that allows users to logout and invalidate their JWT token.
31+
- No server-side token invalidation is required; the client should handle token deletion.
32+
- Return a success response with a status code indicating successful logout (e.g., 204 No Content).

week3/prep-exercise/app.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import express from "express";
2+
// Use below import statement for importing middlewares from users.js for your routes
3+
// import { ....... } from "./users.js";
4+
5+
let app = express();
6+
7+
app.use(express.json());
8+
// Create routes here, e.g. app.post("/register", .......)
9+
10+
app.listen(3000, () => {
11+
console.log("Server is running on port 3000");
12+
});

week3/build-with-students/package.json renamed to week3/prep-exercise/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
"author": "",
1212
"license": "ISC",
1313
"dependencies": {
14-
"bcrypt": "^5.1.1",
1514
"express": "^4.18.2",
16-
"uuid": "^9.0.1"
1715
}
18-
}
16+
}

week3/prep-exercise/users.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Create middlewares required for routes defined in app.js
2+
// export const register = async (req, res) => {};
3+
4+
// You can also create helper functions in this file to help you implement logic inside middlewares

0 commit comments

Comments
 (0)