Files
marketplaces/src/app/components/telegram-login/telegram-login.component.ts

57 lines
1.9 KiB
TypeScript
Raw Normal View History

import { Component, ChangeDetectionStrategy, inject, effect, OnDestroy } from '@angular/core';
2026-02-28 17:18:24 +04:00
import { AuthService } from '../../services/auth.service';
import { TranslatePipe } from '../../i18n/translate.pipe';
import { QrLoginEngine } from '../../shared/qr-login/qr-login.engine';
import { QrLoginAdapter } from '../../shared/qr-login/qr-login.model';
import { AuthSession } from '../../models/auth.model';
2026-02-28 17:18:24 +04:00
@Component({
selector: 'app-telegram-login',
imports: [TranslatePipe],
templateUrl: './telegram-login.component.html',
styleUrls: ['./telegram-login.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
2026-03-25 15:32:50 +04:00
export class TelegramLoginComponent implements OnDestroy {
2026-02-28 17:18:24 +04:00
private authService = inject(AuthService);
showDialog = this.authService.showLoginDialog;
status = this.authService.status;
2026-03-25 15:32:50 +04:00
private readonly adapter: QrLoginAdapter<AuthSession> = {
createSession: () => this.authService.createWebSession(),
checkSessionOnce: webSessionID => this.authService.checkSessionOnce(webSessionID),
isSessionActive: session => !!session?.active,
getAppLoginUrl: webSessionID => this.authService.getTelegramAppLoginUrl(webSessionID),
onLoginComplete: () => this.authService.onTelegramLoginComplete(),
2026-06-20 14:40:22 +04:00
};
2026-02-28 17:18:24 +04:00
private readonly engine = new QrLoginEngine<AuthSession>(this.adapter);
readonly loginUrl = this.engine.loginUrl;
readonly webSessionID = this.engine.webSessionID;
readonly qrStatus = this.engine.qrStatus;
readonly encodedQrUrl = this.engine.encodedQrUrl;
readonly awaitingTelegramReturn = this.engine.awaitingAppReturn;
2026-06-20 14:40:22 +04:00
constructor() {
effect(() => this.engine.setActive(this.showDialog()));
2026-02-28 17:18:24 +04:00
}
ngOnDestroy(): void {
this.engine.destroy();
2026-02-28 17:18:24 +04:00
}
close(): void {
this.engine.setActive(false);
2026-02-28 17:18:24 +04:00
this.authService.hideLogin();
}
openTelegramLogin(): void {
this.engine.openAppLogin();
2026-02-28 17:18:24 +04:00
}
2026-03-25 15:32:50 +04:00
refreshQr(): void {
this.engine.refresh();
2026-06-20 14:40:22 +04:00
}
2026-02-28 17:18:24 +04:00
}