parent
fe46e5d667
commit
1f3b312675
11
src/app.ts
11
src/app.ts
|
@ -1,17 +1,18 @@
|
|||
import "reflect-metadata";
|
||||
import * as dotenvSafe from "dotenv-safe";
|
||||
import { createExpressServer } from "routing-controllers";
|
||||
import consola from "consola";
|
||||
import loaders from "./loaders/index";
|
||||
import * as dotenvSafe from "dotenv-safe";
|
||||
import "reflect-metadata";
|
||||
import { createExpressServer } from "routing-controllers";
|
||||
import authchecker from "./authchecker";
|
||||
import loaders from "./loaders/index";
|
||||
import { ErrorHandler } from './middlewares/ErrorHandler';
|
||||
import { JWTAuth } from './middlewares/JWTAuth';
|
||||
|
||||
dotenvSafe.config();
|
||||
const PORT = process.env.APP_PORT || 4010;
|
||||
|
||||
const app = createExpressServer({
|
||||
authorizationChecker: authchecker,
|
||||
middlewares: [ErrorHandler],
|
||||
middlewares: [ErrorHandler, JWTAuth],
|
||||
development: process.env.NODE_ENV === "production",
|
||||
cors: true,
|
||||
routePrefix: "/api",
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue