2022-03-05 22:57:23 +04:00
|
|
|
import config from 'config';
|
|
|
|
import { NextFunction, Request, Response } from 'express';
|
|
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware for checking jwt token
|
|
|
|
*
|
|
|
|
* @param req - request object
|
|
|
|
* @param res - response object
|
|
|
|
* @param next - next function
|
|
|
|
*/
|
|
|
|
export default async function verifyToken(req: Request, res: Response, next: NextFunction): Promise<void> {
|
|
|
|
const token = req.cookies.authToken;
|
|
|
|
|
|
|
|
try {
|
2022-04-24 16:54:36 +05:30
|
|
|
if (!process.env.PASSWORD) {
|
2022-03-05 22:57:23 +04:00
|
|
|
res.locals.isAuthorized = false;
|
|
|
|
next();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:54:36 +05:30
|
|
|
const decodedToken = jwt.verify(token, process.env.PASSWORD + config.get('secret'));
|
2022-03-05 22:57:23 +04:00
|
|
|
|
|
|
|
res.locals.isAuthorized = !!decodedToken;
|
|
|
|
|
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
res.locals.isAuthorized = false;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|