deno.land / x / alosaur@v1.1.1 / examples / hooks / decorators / authorize-hook.decorator.ts

authorize-hook.decorator.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { BusinessType, Content, getMetadataArgsStorage, HookTarget, HttpContext } from "alosaur/mod.ts";import { getQueryParams } from "alosaur/src/route/get-action-params.ts";import { SLContainer } from "../../../src/di/mod.ts";
type AuthorizeRoleType = string | undefined;
/** * Authorize decorator with role */export function Authorize(role?: AuthorizeRoleType): Function { return function (object: any, methodName: string) { getMetadataArgsStorage().hooks.push({ type: methodName ? BusinessType.Action : BusinessType.Controller, object, target: object.constructor, method: methodName, instance: SLContainer.create(AutorizeHook), payload: role, }); };}
export class AutorizeHook implements HookTarget<unknown, AuthorizeRoleType> { onPreAction(context: HttpContext<unknown>, role: AuthorizeRoleType) { const queryParams = getQueryParams(context.request.url);
if (queryParams == undefined || queryParams.get("role") !== role) { context.response.result = Content({ error: { token: false } }, 403); context.response.setImmediately(); } }}
alosaur

Version Info

Tagged at
a year ago