This commit is contained in:
sdarbinyan
2026-06-01 00:47:26 +04:00
parent 49f69f6af0
commit 4d8dc6b59c
22 changed files with 1266 additions and 353 deletions

View File

@@ -19,10 +19,11 @@ export class TelegramLoginComponent implements OnDestroy {
status = this.authService.status;
loginUrl = signal('');
qrToken = signal('');
webSessionID = signal('');
qrStatus = signal<'loading' | 'ready' | 'expired' | 'error'>('loading');
encodedQrUrl = computed(() => encodeURIComponent(this.loginUrl()));
private readonly pollIntervalMs = 5000;
private pollTimer?: ReturnType<typeof setInterval>;
constructor() {
@@ -45,12 +46,14 @@ export class TelegramLoginComponent implements OnDestroy {
}
openTelegramLogin(): void {
window.open(this.loginUrl(), '_blank');
const url = this.loginUrl();
if (!url) return;
window.open(url, '_blank');
if (!this.pollTimer) {
if (this.qrToken()) {
this.startPolling(this.qrToken());
} else {
this.startSessionPolling();
const webSessionID = this.webSessionID();
if (webSessionID) {
this.startPolling(webSessionID);
}
}
}
@@ -62,43 +65,25 @@ export class TelegramLoginComponent implements OnDestroy {
private initQrLogin(): void {
this.qrStatus.set('loading');
this.authService.createQrToken().subscribe({
this.loginUrl.set('');
this.webSessionID.set('');
this.authService.createWebSession().subscribe({
next: (res) => {
this.loginUrl.set(res.url);
this.qrToken.set(res.token);
this.webSessionID.set(res.webSessionID);
this.qrStatus.set('ready');
this.startPolling(res.token);
this.startPolling(res.webSessionID);
},
error: () => {
this.loginUrl.set(this.authService.getTelegramLoginUrl());
this.qrStatus.set('error');
this.startSessionPolling();
}
});
}
private startSessionPolling(): void {
private startPolling(webSessionID: string): void {
this.stopPolling();
let checks = 0;
this.pollTimer = setInterval(() => {
checks++;
if (checks > 100) {
this.stopPolling();
this.qrStatus.set('expired');
return;
}
this.authService.checkSessionOnce().subscribe(session => {
if (session && session.active) {
this.stopPolling();
this.syncCartAndComplete(session.sessionId);
}
});
}, 3000);
}
private startPolling(token: string): void {
this.stopPolling();
if (!token) return;
if (!webSessionID) return;
let checks = 0;
this.pollTimer = setInterval(() => {
@@ -109,28 +94,18 @@ export class TelegramLoginComponent implements OnDestroy {
return;
}
this.authService.pollQrToken(token).subscribe({
next: (res) => {
switch (res.status) {
case 'confirmed':
this.stopPolling();
if (res.session) {
this.syncCartAndComplete(res.session.sessionId);
} else {
this.authService.onTelegramLoginComplete();
}
break;
case 'expired':
this.stopPolling();
this.qrStatus.set('expired');
break;
this.authService.checkSessionOnce(webSessionID).subscribe({
next: (session) => {
if (session?.active) {
this.stopPolling();
this.syncCartAndComplete(session.sessionId);
}
},
error: () => {
// Network error — keep polling
}
});
}, 3000);
}, this.pollIntervalMs);
}
private syncCartAndComplete(sessionId: string): void {