File tree Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Original file line number Diff line number Diff line change 11import { NextFunction , Request , Response } from "express" ;
2+ import { logger } from "../logger" ;
3+ const JWT_SECRET = process . env . JWT_SECRET ;
4+ const jwt = require ( "jsonwebtoken" ) ;
25
36export 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}
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments