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.
32 lines
715 B
TypeScript
32 lines
715 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 {
|
|
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);
|
|
};
|