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 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 authchecker from "./authchecker";
|
||||||
|
import loaders from "./loaders/index";
|
||||||
import { ErrorHandler } from './middlewares/ErrorHandler';
|
import { ErrorHandler } from './middlewares/ErrorHandler';
|
||||||
|
import { JWTAuth } from './middlewares/JWTAuth';
|
||||||
|
|
||||||
dotenvSafe.config();
|
dotenvSafe.config();
|
||||||
const PORT = process.env.APP_PORT || 4010;
|
const PORT = process.env.APP_PORT || 4010;
|
||||||
|
|
||||||
const app = createExpressServer({
|
const app = createExpressServer({
|
||||||
authorizationChecker: authchecker,
|
authorizationChecker: authchecker,
|
||||||
middlewares: [ErrorHandler],
|
middlewares: [ErrorHandler, JWTAuth],
|
||||||
development: process.env.NODE_ENV === "production",
|
development: process.env.NODE_ENV === "production",
|
||||||
cors: true,
|
cors: true,
|
||||||
routePrefix: "/api",
|
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 * as jwt from "jsonwebtoken";
|
||||||
|
import {
|
||||||
|
ExpressMiddlewareInterface, Middleware
|
||||||
|
} from "routing-controllers";
|
||||||
|
|
||||||
export default (req: Request, res: Response, next: NextFunction) => {
|
@Middleware({ type: "before" })
|
||||||
const token = <string>req.headers["auth"];
|
export class JWTAuth implements ExpressMiddlewareInterface {
|
||||||
try {
|
use(request: any, response: any, next?: (err?: any) => any): any {
|
||||||
const jwtPayload = <any>jwt.verify(token, "secretjwtsecret");
|
const token = <string>request.headers["auth"];
|
||||||
// const jwtPayload = <any>jwt.verify(token, process.env.JWT_SECRET);
|
try {
|
||||||
res.locals.jwtPayload = jwtPayload;
|
const jwtPayload = <any>jwt.verify(token, "secretjwtsecret");
|
||||||
} catch (error) {
|
// const jwtPayload = <any>jwt.verify(token, process.env.JWT_SECRET);
|
||||||
console.log(error);
|
response.locals.jwtPayload = jwtPayload;
|
||||||
return res.status(401).send();
|
} catch (error) {
|
||||||
}
|
console.log(error);
|
||||||
next();
|
return response.status(401).send();
|
||||||
};
|
}
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user