import { Component, ChangeDetectionStrategy, inject, signal, OnInit, OnDestroy } from '@angular/core'; import { AuthService } from '../../services/auth.service'; import { TranslatePipe } from '../../i18n/translate.pipe'; @Component({ selector: 'app-telegram-login', imports: [TranslatePipe], templateUrl: './telegram-login.component.html', styleUrls: ['./telegram-login.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush }) export class TelegramLoginComponent implements OnInit, OnDestroy { private authService = inject(AuthService); showDialog = this.authService.showLoginDialog; status = this.authService.status; loginUrl = signal(''); private pollTimer?: ReturnType; ngOnInit(): void { this.loginUrl.set(this.authService.getTelegramLoginUrl()); } ngOnDestroy(): void { this.stopPolling(); } close(): void { this.authService.hideLogin(); this.stopPolling(); } /** Open Telegram login link and start polling for session */ openTelegramLogin(): void { window.open(this.loginUrl(), '_blank'); this.startPolling(); } /** Start polling the backend to detect when user completes Telegram auth */ private startPolling(): void { this.stopPolling(); // Check every 3 seconds for up to 5 minutes let checks = 0; this.pollTimer = setInterval(() => { checks++; if (checks > 100) { // 100 * 3s = 5 min this.stopPolling(); return; } this.authService.checkSession(); // If authenticated, stop polling and close dialog if (this.authService.isAuthenticated()) { this.stopPolling(); this.authService.hideLogin(); } }, 3000); } private stopPolling(): void { if (this.pollTimer) { clearInterval(this.pollTimer); this.pollTimer = undefined; } } }