Authentication and authorization
This commit is contained in:
committed by
Andrew Trieu
parent
8dd54efa6d
commit
ab526cca2a
15
server/middleware/auth.js
Normal file
15
server/middleware/auth.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
export const verifyToken = (req, res, next) => {
|
||||
try {
|
||||
let token = req.header("Authorization");
|
||||
if (!token) return res.status(403).json({ error: "Unauthorized" });
|
||||
token = token.split(" ")[1];
|
||||
|
||||
const verifiedToken = jwt.verify(token, process.env.JWT_SECRET);
|
||||
req.user = verifiedToken;
|
||||
next();
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user