Auth: always populate req.models.user, thus reducing db requests

This commit is contained in:
Alice Gaudon 2020-08-30 17:31:51 +02:00
parent 28349b791e
commit fae5c68cd0
2 changed files with 2 additions and 5 deletions

View File

@ -1,6 +1,6 @@
{
"name": "wms-core",
"version": "0.21.2",
"version": "0.21.3",
"description": "Node web application framework and toolbelt.",
"repository": "https://gitlab.com/ArisuOngaku/wms-core",
"author": "Alice Gaudon <alice@gaudon.pro>",

View File

@ -15,7 +15,7 @@ export default class AuthComponent extends ApplicationComponent<void> {
public async init(router: Router): Promise<void> {
router.use(async (req, res, next) => {
req.authGuard = this.authGuard;
res.locals.user = await (await req.authGuard.getProof(req))?.getResource();
req.models.user = res.locals.user = await (await req.authGuard.getProof(req))?.getResource();
next();
});
}
@ -31,7 +31,6 @@ export const REQUIRE_REQUEST_AUTH_MIDDLEWARE = async (req: Request, res: Respons
return;
}
req.models.user = await proof.getResource();
next();
};
@ -39,7 +38,6 @@ export const REQUIRE_AUTH_MIDDLEWARE = async (req: Request, res: Response, next:
// Via request
let proof = await req.authGuard.isAuthenticatedViaRequest(req);
if (proof) {
req.models.user = await proof.getResource();
next();
return;
}
@ -54,7 +52,6 @@ export const REQUIRE_AUTH_MIDDLEWARE = async (req: Request, res: Response, next:
return;
}
req.models.user = await proof.getResource();
next();
};
export const REQUIRE_GUEST_MIDDLEWARE = async (req: Request, res: Response, next: NextFunction): Promise<void> => {