Skip to content

Commit 77c8875

Browse files
committed
Typescript Bootcamp
1 parent 95b5d95 commit 77c8875

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
11
import {NextFunction, Request, Response} from "express";
2+
import {logger} from "../logger";
3+
const JWT_SECRET = process.env.JWT_SECRET;
4+
const jwt = require("jsonwebtoken");
25

36
export function checkIfAuthenticated(
4-
request: Request, response: Response, next:NextFunction
5-
) {
7+
request: Request, response: Response, next:NextFunction) {
68

9+
const authJwtToken = request.headers.authorization;
710

11+
if (!authJwtToken) {
12+
logger.info(`The authentication JWT is not present, access denied.`);
13+
response.sendStatus(403);
14+
return;
15+
}
16+
17+
checkJwtValidity(authJwtToken)
18+
.then(user => {
19+
20+
logger.info(`Authentication JWT successfully decoded:`, user);
21+
request["user"] = user;
22+
23+
next();
24+
})
25+
.catch(err => {
26+
logger.error(`Could not validate the authentication JWT, access denied.`, err);
27+
response.sendStatus(403);
28+
});
29+
}
30+
31+
async function checkJwtValidity(authJwtToken:string) {
32+
33+
const user = await jwt.verify(authJwtToken, JWT_SECRET);
34+
35+
logger.info("Found user details in JWT:", user);
36+
37+
return user;
838
}

rest-api/src/routes/get-all-courses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export async function getAllCourses(
88

99
try {
1010

11-
logger.debug(`Called getAllCourses()`);
11+
logger.debug(`Called getAllCourses()`, request["user"]);
1212

1313
const courses = await AppDataSource
1414
.getRepository(Course)

0 commit comments

Comments
 (0)