Files
x-news-ai/src/modules/telegram/guard/auth.guard.ts
T
2026-05-14 08:42:03 +00:00

25 lines
844 B
TypeScript

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