NestJS guard crashing server when canActivate is false #1224
Unanswered
SergioArrighi
asked this question in
Q&A
Replies: 3 comments
-
Any solutions? |
Beta Was this translation helpful? Give feedback.
0 replies
-
I've fixed this problem this way. May be useful to someone else?
export abstract class SafeGuardInterceptor
implements NestInterceptor, CanActivate
{
abstract canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean>;
async intercept(context: ExecutionContext, next: CallHandler<any>) {
let result = false;
const returnValue = this.canActivate(context);
if (isObservable(returnValue)) {
returnValue.subscribe((value) => {
if (value) {
return next.handle();
} else {
return of(undefined);
}
});
return of(undefined);
} else {
result = await returnValue;
if (result) {
return next.handle();
}
return of(undefined);
}
}
}
type Class<I, Args extends any[] = any[]> = new (...args: Args) => I;
export const UseSafeGuards = (...arr: Class<SafeGuardInterceptor>[]) =>
UseInterceptors(...arr);
@Injectable()
export class AdminGuard extends SafeGuardInterceptor {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const telegraf = context.getArgByIndex<TelegrafContext>(0);
// user is a property injected by some middleware, any logic can be used
return !!telegraf.user?.isAdmin;
}
} And used it like other Guards, for @Update()
@UseSafeGuards(AdminGuard)
export class AdminUpdate {
// commands or other stuff here
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
based on the sample given, it seems only can use |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello guys,
I am facing an issue while trying to implement a NestJS guard.
Requirement
In the .env file I have a list of usernames to be considered as admin.
Some of the commands can only be used by admins.
Solution
Add a IsAdminGuard checking if the incoming from.username is contained in the admins array.
Error
throw new common_1.ForbiddenException(constants_2.FORBIDDEN_MESSAGE); ^ ForbiddenException: Forbidden resource at canActivateFn (/Users/sergioarrighi/Documents/workspace/nordic-whales-bot/node_modules/@nestjs/core/helpers/external-context-creator.js:157:23) at processTicksAndRejections (node:internal/process/task_queues:96:5) at target (/Users/sergioarrighi/Documents/workspace/nordic-whales-bot/node_modules/@nestjs/core/helpers/external-context-creator.js:73:31)
Expected behaviour
The request gets rejected and server stays operational
To reproduce you can use my guthub repo.
https://github.com/SergioArrighi/nordic-whales-bot
The main service is here.
While the guard is this one..
To reproduce
TELEGRAM_BOT_TOKEN=
ADMINS=[,,...]
Thanks for the terrific job and best regards
Beta Was this translation helpful? Give feedback.
All reactions