Skip to content
Snippets Groups Projects
Commit 690cfff7 authored by michael.minelli's avatar michael.minelli
Browse files

Session => Add response if the token read fail

parent 2f5fb61a
No related branches found
No related tags found
No related merge requests found
import { getReasonPhrase } from 'http-status-codes';
import { getReasonPhrase, StatusCodes } from 'http-status-codes';
import * as jwt from 'jsonwebtoken';
import { JwtPayload } from 'jsonwebtoken';
import Config from '../config/Config';
......@@ -22,7 +22,7 @@ class Session {
constructor() { }
async initSession(req: express.Request) {
async initSession(req: express.Request, res: express.Response) {
const authorization = req.headers.authorization;
if ( authorization ) {
if ( authorization.startsWith('Bearer ') ) {
......@@ -35,7 +35,9 @@ class Session {
this.profile = jwtData.profile;
this.profile = await UserManager.getById(this.profile.id!) ?? this.profile;
}
} catch ( err ) { }
} catch ( err ) {
res.sendStatus(StatusCodes.UNAUTHORIZED).end();
}
}
}
}
......
......@@ -6,7 +6,7 @@ class SessionMiddleware {
register(): (req: express.Request, res: express.Response, next: express.NextFunction) => void {
return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
req.session = new Session();
await req.session.initSession(req);
await req.session.initSession(req, res);
return next();
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment