first commit
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
// auth.guard.ts
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import { TelegrafExecutionContext } from 'nestjs-telegraf';
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuard implements CanActivate {
|
||||
// Danh sách ID được phép (có thể để trong file .env)
|
||||
private readonly allowedIds =(process.env.TELEGRAM_ALLOW_CHAT_IDS || '').split(',');
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
console.log('AuthGuard');
|
||||
const ctx = TelegrafExecutionContext.create(context);
|
||||
const { chat } = ctx.getContext();
|
||||
|
||||
// Kiểm tra nếu chat_id nằm trong danh sách cho phép
|
||||
const isAllowed = this.allowedIds.includes(chat.id);
|
||||
|
||||
if (!isAllowed) {
|
||||
console.warn(`Truy cập trái phép từ ID: ${chat.id}`);
|
||||
}
|
||||
|
||||
return isAllowed;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user