From 3b3d26ed3d4540905666c0043c5d33bf44dcb249 Mon Sep 17 00:00:00 2001 From: Mauro Amico Date: Tue, 6 Feb 2024 00:03:15 +1030 Subject: [PATCH] fix: verify token expired (#2) * fix: verify token expired * debug condition * debug * debug * debug * fix typo * fix condition * debug --------- Co-authored-by: Andrea Cecchi --- src/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 6c17912..606f11c 100644 --- a/src/index.js +++ b/src/index.js @@ -40,9 +40,19 @@ const applyConfig = (config) => { } if (token && settings?.userHeaderName) { const user = req.get(settings.userHeaderName); - if (user && jwtDecode(token).sub !== user) { + // require auth if: + // - header user is different from token user + // - token has no expiration + // - token is expired + console.log("USER: ", user); + console.log("TOKEN: ", jwtDecode(token)); + console.log("TOKEN SCADUTO: ", jwtDecode(token).exp < Date.now() / 1000); + console.log("CONDIZIONE: ", ((user && jwtDecode(token).sub !== user) || !jwtDecode(token).exp || jwtDecode(token).exp < Date.now() / 1000)); + if ((user && jwtDecode(token).sub !== user) || !jwtDecode(token).exp || jwtDecode(token).exp < Date.now() / 1000){ + // TODO: eventually add base_url to a relative settings.loginUrl return res.redirect(`${settings.loginUrl}?came_from=${req.url}`); } + } } return next();