mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-30 12:27:39 +03:00
feat!(): add custom execution context
This commit is contained in:
21
sample/common/guards/admin.guard.ts
Normal file
21
sample/common/guards/admin.guard.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import { TelegrafExecutionContext } from '../../../lib/execution-context/telegraf-execution-context';
|
||||
import { Context } from '../../interfaces/context.interface';
|
||||
import { TelegrafException } from '../../../lib/errors';
|
||||
|
||||
@Injectable()
|
||||
export class AdminGuard implements CanActivate {
|
||||
private readonly ADMIN_IDS = [];
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const ctx = TelegrafExecutionContext.create(context);
|
||||
const { from } = ctx.getContext<Context>();
|
||||
|
||||
const isAdmin = this.ADMIN_IDS.includes(from.id);
|
||||
if (!isAdmin) {
|
||||
throw new TelegrafException('You are not admin >:(');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user