update user, auth, token, roles, done

This commit is contained in:
2024-11-26 23:58:38 +07:00
parent 6e71588f91
commit 9608e96401
27 changed files with 1842 additions and 152 deletions

38
src/helpers/jwt.ts Normal file
View 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, 401, 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, 401, Language.lang.failed_access);
}
return next();
});
}
}