Skip to content

Commit

Permalink
feat(guard): not throw error in guard default
Browse files Browse the repository at this point in the history
  • Loading branch information
jiawei397 committed Dec 23, 2021
1 parent 8488ccd commit c537009
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
Router as OriginRouter,
send,
Status,
STATUS_TEXT,
} from "https://deno.land/x/[email protected]/mod.ts";

export type {
Expand Down
2 changes: 2 additions & 0 deletions example/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getMetadataForGuard,
Injectable,
Reflector,
UnauthorizedException,
} from "../../mod.ts";
import { delay } from "../deps.ts";
import { RoleService } from "../user/services/role.service.ts";
Expand All @@ -23,6 +24,7 @@ export class AuthGuard implements CanActivate {
this.reflector.get<string[]>("roles", context),
);
await delay(10);
// throw new UnauthorizedException("Unauthorized");
// throw new ForbiddenException("this is AuthGuard error message");
return true;
// return false;
Expand Down
3 changes: 2 additions & 1 deletion src/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ export async function checkByGuard(
Reflect.defineMetadata(META_FUNCTION_KEY, fn, context); // record the function to context
const result = await _guard.canActivate(context);
if (!result) {
throw new UnauthorizedException(UnauthorizedException.name);
return false; // will not continue to next guard and not throw an exception
}
}
}
return true;
}

export function SetMetadata<K = string, V = any>(
Expand Down
9 changes: 8 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
OriginRouter,
red,
Reflect,
Status,
STATUS_TEXT,
yellow,
} from "../deps.ts";
import { checkByGuard } from "./guard.ts";
Expand Down Expand Up @@ -125,7 +127,12 @@ export class Router extends OriginRouter {
const methodKey = join(modelPath, route);
const funcStart = Date.now();
this[methodType.toLowerCase()](methodKey, async (context: Context) => {
await checkByGuard(instance, fn, context);
const guardResult = await checkByGuard(instance, fn, context);
if (!guardResult) {
context.response.status = Status.Forbidden;
context.response.body = STATUS_TEXT.get(Status.Forbidden);
return;
}
const args = await transferParam(instance, methodName, context);
const result = await checkByInterceptors(
context,
Expand Down
1 change: 1 addition & 0 deletions src/router_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Deno.test("join", () => {
assertEquals(join("/api", "/user", "add/"), "/api/user/add");
assertEquals(join("/api", "/user/", "add/"), "/api/user/add");
assertEquals(join("/api", "/user/", "/add"), "/api/user/add");
assertEquals(join("/api", "/user/", "/add/"), "/api/user/add");
});

Deno.test("mapRoute without controller route", async () => {
Expand Down

0 comments on commit c537009

Please sign in to comment.