nestjs-telegraf/sample/common/guards/admin.guard.ts
2021-01-03 01:30:57 +03:00

22 lines
720 B
TypeScript

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;
}
}