front and back work

This commit is contained in:
QkoSad
2023-07-29 18:29:41 +03:00
parent 40051f9d5e
commit 2fee49e583
9 changed files with 29 additions and 26 deletions
+6 -2
View File
@@ -2,7 +2,9 @@ import config from 'config'
import jwt from 'jsonwebtoken'
import type { Request, Response, NextFunction } from 'express';
function auth(req: Request, res: Response, next: NextFunction) {
interface ResponseAndUser extends Request { user: string }
function auth(req: ResponseAndUser, res: Response, next: NextFunction) {
// Get token from header
const token = req.header('x-auth-token');
@@ -11,13 +13,14 @@ function auth(req: Request, res: Response, next: NextFunction) {
return res.status(401).json({ msg: 'No token, authorization denied' });
}
console.log('auth middlewarwe')
// Verify token
try {
jwt.verify(token, config.get('jwtSecret'), (error, decoded) => {
if (error) {
return res.status(401).json({ msg: 'Token is not valid' });
} else {
if ('user' in req && decoded && typeof decoded !== "string")
if (decoded && typeof decoded !== "string")
req.user = decoded?.user;
next();
}
@@ -29,3 +32,4 @@ function auth(req: Request, res: Response, next: NextFunction) {
};
export default auth