diff --git a/login-system/token.js b/login-system/token.js index 0818c76..de5466a 100644 --- a/login-system/token.js +++ b/login-system/token.js @@ -1,27 +1,49 @@ require("dotenv").config() -// generating the token for user const jwt = require("jsonwebtoken") -const crypto=require('crypto') -function generateAccessToken (user) { -return jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, {expiresIn: "20m"}) +const crypto=require('crypto'); +const ACCESS_TOKEN = process.env.ACCESS_TOKEN_SECRET + +// check for valid access token +if(!ACCESS_TOKEN){ + throw new Error("Missing ACCESS_TOKEN_SECRET in environment variables."); +} +// function for generating access token +function generateAccessToken(user,expireTime = "20m") { + try{ + const token = jwt.sign(user,ACCESS_TOKEN,{expiresIn : expireTime}); + return token; + }catch(err){ + console.log({ + Message : "Error While Generating The Token", + Error : err.message + }) + return null; + } } // function for decoding the token -function decodeAccessToken(authorizationHeader) { - if (!authorizationHeader) { - console.log('authorization header is missing') - return null; - } +function decodeAccessToken(AuthHeader){ + if(!AuthHeader){ + console.log("Authorization header is missing"); + return null; + } - const token = authorizationHeader.split(' ')[1]; - - try { - const decoded = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET); - // console.log('decoded info: ',decoded) - return decoded; - } catch (error) { - console.error('Error decoding access token:', error); - return null; + const token = AuthHeader.split(" ")[1]; + if(!token){ + console.log("Token is missing"); + return null; + } + try{ + + const decodedToken = jwt.verify(token,ACCESS_TOKEN) + return decodedToken; + + }catch(err){ + console.log({ + Message : "Error decoding access token:", + Error : err.message + }) + return null } } @@ -30,4 +52,4 @@ function registrationToken(email) { return crypto.createHash('sha256').update(email).digest('hex'); } -module.exports={ generateAccessToken, decodeAccessToken ,registrationToken} \ No newline at end of file +module.exports={ generateAccessToken, decodeAccessToken ,registrationToken} diff --git a/public/fac_login.html b/public/fac_login.html index 3b7d28b..ef0be76 100644 --- a/public/fac_login.html +++ b/public/fac_login.html @@ -54,6 +54,14 @@