You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
903 B
TypeScript
31 lines
903 B
TypeScript
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("Missing authorization header")
|
|
|
|
return false
|
|
}
|
|
|
|
try {
|
|
console.log("req.headers.authorization", req.headers.authorization)
|
|
console.log(typeof(req.headers.authorization))
|
|
const token = req.headers.authorization.split(' ')[1]
|
|
console.log("token", token)
|
|
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)
|
|
}
|