From 9be7c7bfb33bf1c3c805abdbef3b0e9e1bf32734 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Fri, 16 Jun 2023 20:29:50 +0200
Subject: [PATCH] Session => Add session refresh

---
 ExpressAPI/src/controllers/Session.ts           | 8 ++++++--
 ExpressAPI/src/middlewares/SessionMiddleware.ts | 3 ++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/ExpressAPI/src/controllers/Session.ts b/ExpressAPI/src/controllers/Session.ts
index cb15769..3baf624 100644
--- a/ExpressAPI/src/controllers/Session.ts
+++ b/ExpressAPI/src/controllers/Session.ts
@@ -5,6 +5,7 @@ import Config              from '../config/Config';
 import express             from 'express';
 import ApiRequest          from '../models/ApiRequest';
 import User                from '../models/User';
+import UserManager         from '../managers/UserManager';
 
 
 class Session {
@@ -19,16 +20,19 @@ class Session {
         this._profile = newProfile;
     }
 
-    constructor(req: ApiRequest) {
+    constructor() { }
+
+    async initSession(req: ApiRequest) {
         const authorization = req.headers.authorization;
         if ( authorization ) {
             const jwtToken = authorization.replace('Bearer ', '');
 
             try {
-                const jwtData = jwt.verify(jwtToken, Config.jwtSecretKey) as JwtPayload;
+                const jwtData = jwt.verify(jwtToken, Config.jwtConfig.secret) as JwtPayload;
 
                 if ( jwtData.profile ) {
                     this.profile.importFromJsonObject(jwtData.profile);
+                    this.profile = await UserManager.getById(this.profile.userID);
                     this.profile.currentSession = this;
                 }
             } catch ( err ) { }
diff --git a/ExpressAPI/src/middlewares/SessionMiddleware.ts b/ExpressAPI/src/middlewares/SessionMiddleware.ts
index 49fd611..664edcb 100644
--- a/ExpressAPI/src/middlewares/SessionMiddleware.ts
+++ b/ExpressAPI/src/middlewares/SessionMiddleware.ts
@@ -18,7 +18,8 @@ class SessionMiddleware {
 
     register(): (req: ApiRequest, res: express.Response, next: express.NextFunction) => void {
         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();
         };
-- 
GitLab