exported token functions to utils
parent
728a0a59db
commit
54fa9ac7fa
@ -0,0 +1,27 @@
|
||||
import jwt, {Secret} from "jsonwebtoken"
|
||||
import {User} from "../models/user"
|
||||
import {NextFunction, Request, Response} from "express"
|
||||
|
||||
const SECRET = process.env.TOKEN_SECRET as Secret
|
||||
|
||||
export const verifyAuthToken = (req: Request, res: Response, next: NextFunction) => {
|
||||
if (!req.headers.authorization) {
|
||||
res.status(401)
|
||||
res.json("Access denied, invalid token")
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
const token = req.headers.authorization.split(" ")[1]
|
||||
jwt.verify(token, SECRET)
|
||||
next()
|
||||
} catch (err) {
|
||||
res.status(401)
|
||||
res.json("Access denied, invalid token")
|
||||
}
|
||||
}
|
||||
|
||||
export const verifyUserToken = (user: User | null) => {
|
||||
return jwt.sign({ user }, SECRET)
|
||||
}
|
Loading…
Reference in New Issue