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", "name": "wms-core",
"version": "0.21.2", "version": "0.21.3",
"description": "Node web application framework and toolbelt.", "description": "Node web application framework and toolbelt.",
"repository": "https://gitlab.com/ArisuOngaku/wms-core", "repository": "https://gitlab.com/ArisuOngaku/wms-core",
"author": "Alice Gaudon <alice@gaudon.pro>", "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> { public async init(router: Router): Promise<void> {
router.use(async (req, res, next) => { router.use(async (req, res, next) => {
req.authGuard = this.authGuard; 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(); next();
}); });
} }
@ -31,7 +31,6 @@ export const REQUIRE_REQUEST_AUTH_MIDDLEWARE = async (req: Request, res: Respons
return; return;
} }
req.models.user = await proof.getResource();
next(); next();
}; };
@ -39,7 +38,6 @@ export const REQUIRE_AUTH_MIDDLEWARE = async (req: Request, res: Response, next:
// Via request // Via request
let proof = await req.authGuard.isAuthenticatedViaRequest(req); let proof = await req.authGuard.isAuthenticatedViaRequest(req);
if (proof) { if (proof) {
req.models.user = await proof.getResource();
next(); next();
return; return;
} }
@ -54,7 +52,6 @@ export const REQUIRE_AUTH_MIDDLEWARE = async (req: Request, res: Response, next:
return; return;
} }
req.models.user = await proof.getResource();
next(); next();
}; };
export const REQUIRE_GUEST_MIDDLEWARE = async (req: Request, res: Response, next: NextFunction): Promise<void> => { export const REQUIRE_GUEST_MIDDLEWARE = async (req: Request, res: Response, next: NextFunction): Promise<void> => {