Authentification Token

This commit is contained in:
2024-11-14 15:32:28 +01:00
parent f4d5f54846
commit c61a628ed4
10 changed files with 283 additions and 58 deletions

View File

@@ -0,0 +1,22 @@
import { NextFunction, Request, Response } from "express";
import jwt from "jsonwebtoken"
export function verifyToken(req: Request, res: Response, next: NextFunction) {
const token = req.header("Authorization")
if(!token) {
return res.status(401).json({
error: "Access denied"
})
}
try {
const decoded = jwt.verify(token, 'sjcucjdkdf')
req["id"] = decoded["userId"]
next()
} catch(error) {
res.status(401).json({
error: "Invalid token"
})
}
}