init
This commit is contained in:
18
backend/middleware/auth.js
Normal file
18
backend/middleware/auth.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
export const validateToken = async (req, res, next) => {
|
||||
|
||||
const authHeader = req.headers.authorization
|
||||
|
||||
if (!authHeader)
|
||||
return res.status(401).json({ error: "Invalid token" })
|
||||
const token = authHeader.split(' ')[1]
|
||||
try {
|
||||
req.user = jwt.verify(token, process.env.JWT_SECRET)
|
||||
} catch (error) {
|
||||
return res.status(401).json({ error: "Invalid token" })
|
||||
}
|
||||
|
||||
|
||||
next()
|
||||
}
|
||||
Reference in New Issue
Block a user