mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-07-21 06:09:41 +02:00
39 lines
878 B
TypeScript
39 lines
878 B
TypeScript
|
import * as dotenv from 'dotenv';
|
||
|
import config from 'config';
|
||
|
import { NextFunction, Request, Response } from 'express';
|
||
|
import jwt from 'jsonwebtoken';
|
||
|
import Users from '../../controllers/users';
|
||
|
|
||
|
dotenv.config();
|
||
|
|
||
|
/**
|
||
|
* 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 {
|
||
|
const userDoc = await Users.get();
|
||
|
|
||
|
if (!userDoc.passHash) {
|
||
|
res.locals.isAuthorized = false;
|
||
|
next();
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const decodedToken = jwt.verify(token, userDoc.passHash + config.get('secret'));
|
||
|
|
||
|
res.locals.isAuthorized = !!decodedToken;
|
||
|
|
||
|
next();
|
||
|
} catch (err) {
|
||
|
res.locals.isAuthorized = false;
|
||
|
next();
|
||
|
}
|
||
|
}
|