diff --git a/PROF-XXI FW Tool/server/middleware/auth.js b/PROF-XXI FW Tool/server/middleware/auth.js new file mode 100644 index 0000000000000000000000000000000000000000..0ab58aa5fbaf794279e39ee387a710246e39723f --- /dev/null +++ b/PROF-XXI FW Tool/server/middleware/auth.js @@ -0,0 +1,27 @@ +const jwt = require("jsonwebtoken"); + +const auth = async (req, res, next) => { + try { + //console.log(req.headers.authorization); + const token = req.headers.authorization.split(" ")[1]; + + // Other Tokens Like Google ones have a length > 500 ( Used in case of setting up another authentification system ) + const isCustomAuth = token.length < 500; + + let decodedData; + + if (token && isCustomAuth) { + decodedData = jwt.verify(token, process.env.JWT_SECRET); + + req.userId = decodedData.id; + req.userEmail = decodedData.email; + //console.log(decodedData.email + " Connected !"); + } + + next(); + } catch (err) { + console.log(err); + } +}; + +module.exports = auth;