initial
This commit is contained in:
38
src/helpers/jwt.ts
Normal file
38
src/helpers/jwt.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { NextFunction, Response } from 'express';
|
||||
import { Logger, ILogObj } from 'tslog';
|
||||
import { Request, expressjwt } from "express-jwt";
|
||||
import fs from 'fs';
|
||||
import { Language } from '../langs/lang';
|
||||
import { ReturnHelper } from './express/return';
|
||||
import express from 'express';
|
||||
|
||||
const log: Logger<ILogObj> = new Logger({ name: '[JwtHelper]', type: 'pretty' });
|
||||
|
||||
export default class JwtHelper {
|
||||
static secure = (app: express.Application) => {
|
||||
|
||||
var publicKey = fs.readFileSync("src/helpers/key/public.key");
|
||||
|
||||
app.use(
|
||||
expressjwt({
|
||||
secret: publicKey, algorithms: ["RS256"]
|
||||
}).unless({ path: ["/token"] }),
|
||||
function (req: Request, res: Response, next: NextFunction) {
|
||||
if (!req.auth?.data.id) {
|
||||
return ReturnHelper.errorResponse(res, 403, 666, Language.lang.failed_access);
|
||||
}
|
||||
|
||||
return next();
|
||||
}
|
||||
)
|
||||
|
||||
app.use(function (err: any, req: Request, res: Response, next: NextFunction) {
|
||||
if (err.name === 'UnauthorizedError') {
|
||||
return ReturnHelper.errorResponse(res, 403, 666, Language.lang.failed_access);
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user