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

Session => Add session refresh

parent 937a4611
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ import Config from '../config/Config'; ...@@ -5,6 +5,7 @@ import Config from '../config/Config';
import express from 'express'; import express from 'express';
import ApiRequest from '../models/ApiRequest'; import ApiRequest from '../models/ApiRequest';
import User from '../models/User'; import User from '../models/User';
import UserManager from '../managers/UserManager';
class Session { class Session {
...@@ -19,16 +20,19 @@ class Session { ...@@ -19,16 +20,19 @@ class Session {
this._profile = newProfile; this._profile = newProfile;
} }
constructor(req: ApiRequest) { constructor() { }
async initSession(req: ApiRequest) {
const authorization = req.headers.authorization; const authorization = req.headers.authorization;
if ( authorization ) { if ( authorization ) {
const jwtToken = authorization.replace('Bearer ', ''); const jwtToken = authorization.replace('Bearer ', '');
try { try {
const jwtData = jwt.verify(jwtToken, Config.jwtSecretKey) as JwtPayload; const jwtData = jwt.verify(jwtToken, Config.jwtConfig.secret) as JwtPayload;
if ( jwtData.profile ) { if ( jwtData.profile ) {
this.profile.importFromJsonObject(jwtData.profile); this.profile.importFromJsonObject(jwtData.profile);
this.profile = await UserManager.getById(this.profile.userID);
this.profile.currentSession = this; this.profile.currentSession = this;
} }
} catch ( err ) { } } catch ( err ) { }
......
...@@ -18,7 +18,8 @@ class SessionMiddleware { ...@@ -18,7 +18,8 @@ class SessionMiddleware {
register(): (req: ApiRequest, res: express.Response, next: express.NextFunction) => void { register(): (req: ApiRequest, res: express.Response, next: express.NextFunction) => void {
return async (req: ApiRequest, res: express.Response, next: express.NextFunction) => { return async (req: ApiRequest, res: express.Response, next: express.NextFunction) => {
req.session = new Session(req); req.session = new Session();
await req.session.initSession(req);
return next(); return next();
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment