🚧 basic JWTAuth Middleware

ref #12
This commit is contained in:
2020-12-04 21:38:34 +01:00
parent fe46e5d667
commit 1f3b312675
2 changed files with 24 additions and 20 deletions

View File

@@ -1,17 +1,20 @@
import { Request, Response, NextFunction } from "express";
// import bodyParser from 'body-parser';
// import cors from 'cors';
import * as jwt from "jsonwebtoken";
import {
ExpressMiddlewareInterface, Middleware
} from "routing-controllers";
export default (req: Request, res: Response, next: NextFunction) => {
const token = <string>req.headers["auth"];
try {
const jwtPayload = <any>jwt.verify(token, "secretjwtsecret");
// const jwtPayload = <any>jwt.verify(token, process.env.JWT_SECRET);
res.locals.jwtPayload = jwtPayload;
} catch (error) {
console.log(error);
return res.status(401).send();
}
next();
};
@Middleware({ type: "before" })
export class JWTAuth implements ExpressMiddlewareInterface {
use(request: any, response: any, next?: (err?: any) => any): any {
const token = <string>request.headers["auth"];
try {
const jwtPayload = <any>jwt.verify(token, "secretjwtsecret");
// const jwtPayload = <any>jwt.verify(token, process.env.JWT_SECRET);
response.locals.jwtPayload = jwtPayload;
} catch (error) {
console.log(error);
return response.status(401).send();
}
next();
}
}