Skip to content

Commit

Permalink
middlewares added
Browse files Browse the repository at this point in the history
  • Loading branch information
Auro-jyoti committed Jan 24, 2024
1 parent 5401b50 commit 33248ff
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions backend/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {JWT_SECRET} = require("./config");
const jwt = require("jsonwebtoken");

const authMiddleware = (req, res, next) => {
const authHeader = req.headers.authorization;

if(!authHeader || !authHeader.startWith("Bearer ")) {
return res.status(403).json({});
}

const token = authHeader.split(' ')[1];

try {
const decoded = jwt.verify(token, JWT_SECRET);

req.userId = decoded.userId;

next();
} catch (err) {
return res.stauts(403).json({});
}
};

module.exports ={
authMiddleware
}

0 comments on commit 33248ff

Please sign in to comment.