Skip to content
Snippets Groups Projects
Commit d2c2c2a9 authored by Matt Kirby's avatar Matt Kirby
Browse files

Aut middleware with extra secret token

parent 4825c66d
No related branches found
No related tags found
1 merge request!15Add Friend service
import jwt, { Secret, JwtPayload } from 'jsonwebtoken';
import { Request, Response, NextFunction } from 'express';
export const SECRET_KEY: Secret = 'abcdefg12345';
export interface CustomJWTRequest extends Request {
token?: string | JwtPayload;
}
export const authorize = async (req: Request, res: Response, next: NextFunction) => {
try {
const authHeader = req.headers.authorization;
const token = authHeader?.split(" ")[1];
if (!token) {
throw new Error();
}
const decoded = jwt.verify(token, SECRET_KEY);
(req as CustomJWTRequest).token = decoded;
next();
} catch (err) {
console.log(err)
res.status(401).send('Please authenticate');
}
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment