revert
This commit is contained in:
@@ -19,53 +19,22 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
webSessionID = signal('');
|
webSessionID = signal('');
|
||||||
qrStatus = signal<'loading' | 'ready' | 'expired' | 'error'>('loading');
|
qrStatus = signal<'loading' | 'ready' | 'expired' | 'error'>('loading');
|
||||||
encodedQrUrl = computed(() => encodeURIComponent(this.loginUrl()));
|
encodedQrUrl = computed(() => encodeURIComponent(this.loginUrl()));
|
||||||
awaitingTelegramReturn = signal(false);
|
|
||||||
|
|
||||||
private readonly pollIntervalMs = 5000;
|
private readonly pollIntervalMs = 5000;
|
||||||
private pollTimer?: ReturnType<typeof setInterval>;
|
private pollTimer?: ReturnType<typeof setInterval>;
|
||||||
private mobileFallbackTimeout?: ReturnType<typeof setTimeout>;
|
|
||||||
private readonly handleVisibilityChange = () => {
|
|
||||||
if (document.visibilityState === 'hidden') {
|
|
||||||
this.clearMobileFallbackTimeout();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.resumeLoginPolling();
|
|
||||||
};
|
|
||||||
private readonly handleWindowFocus = () => {
|
|
||||||
this.resumeLoginPolling();
|
|
||||||
};
|
|
||||||
private readonly handlePageShow = () => {
|
|
||||||
this.resumeLoginPolling();
|
|
||||||
};
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
effect(() => {
|
effect(() => {
|
||||||
if (this.showDialog()) {
|
if (this.showDialog()) {
|
||||||
this.initQrLogin();
|
this.initQrLogin();
|
||||||
} else {
|
} else {
|
||||||
this.awaitingTelegramReturn.set(false);
|
|
||||||
this.clearMobileFallbackTimeout();
|
|
||||||
this.stopPolling();
|
this.stopPolling();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
document.addEventListener('visibilitychange', this.handleVisibilityChange);
|
|
||||||
window.addEventListener('focus', this.handleWindowFocus);
|
|
||||||
window.addEventListener('pageshow', this.handlePageShow);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy(): void {
|
ngOnDestroy(): void {
|
||||||
this.clearMobileFallbackTimeout();
|
|
||||||
this.stopPolling();
|
this.stopPolling();
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
|
|
||||||
window.removeEventListener('focus', this.handleWindowFocus);
|
|
||||||
window.removeEventListener('pageshow', this.handlePageShow);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close(): void {
|
close(): void {
|
||||||
@@ -74,21 +43,16 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
openTelegramLogin(): void {
|
openTelegramLogin(): void {
|
||||||
const webSessionID = this.webSessionID();
|
|
||||||
const url = this.loginUrl();
|
const url = this.loginUrl();
|
||||||
if (!url || !webSessionID) return;
|
if (!url) return;
|
||||||
|
|
||||||
|
window.open(url, '_blank');
|
||||||
if (!this.pollTimer) {
|
if (!this.pollTimer) {
|
||||||
this.startPolling(webSessionID);
|
const webSessionID = this.webSessionID();
|
||||||
|
if (webSessionID) {
|
||||||
|
this.startPolling(webSessionID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isMobileBrowser()) {
|
|
||||||
this.awaitingTelegramReturn.set(true);
|
|
||||||
this.launchTelegramOnMobile(webSessionID, url);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
window.open(url, '_blank', 'noopener,noreferrer');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshQr(): void {
|
refreshQr(): void {
|
||||||
@@ -130,8 +94,6 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
this.authService.checkSessionOnce(webSessionID).subscribe({
|
this.authService.checkSessionOnce(webSessionID).subscribe({
|
||||||
next: (session) => {
|
next: (session) => {
|
||||||
if (session?.active) {
|
if (session?.active) {
|
||||||
this.awaitingTelegramReturn.set(false);
|
|
||||||
this.clearMobileFallbackTimeout();
|
|
||||||
this.stopPolling();
|
this.stopPolling();
|
||||||
this.authService.onTelegramLoginComplete();
|
this.authService.onTelegramLoginComplete();
|
||||||
}
|
}
|
||||||
@@ -149,57 +111,4 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
this.pollTimer = undefined;
|
this.pollTimer = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private resumeLoginPolling(): void {
|
|
||||||
if (!this.showDialog() || !this.awaitingTelegramReturn()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const webSessionID = this.webSessionID();
|
|
||||||
if (!webSessionID) {
|
|
||||||
this.awaitingTelegramReturn.set(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.pollTimer) {
|
|
||||||
this.startPolling(webSessionID);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.authService.checkSessionOnce(webSessionID).subscribe(session => {
|
|
||||||
if (session?.active) {
|
|
||||||
this.awaitingTelegramReturn.set(false);
|
|
||||||
this.stopPolling();
|
|
||||||
this.authService.onTelegramLoginComplete();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private launchTelegramOnMobile(webSessionID: string, fallbackUrl: string): void {
|
|
||||||
this.clearMobileFallbackTimeout();
|
|
||||||
this.mobileFallbackTimeout = setTimeout(() => {
|
|
||||||
if (typeof document !== 'undefined' && document.visibilityState === 'visible') {
|
|
||||||
window.location.assign(fallbackUrl);
|
|
||||||
}
|
|
||||||
}, 1200);
|
|
||||||
|
|
||||||
window.location.assign(this.authService.getTelegramAppLoginUrl(webSessionID));
|
|
||||||
}
|
|
||||||
|
|
||||||
private clearMobileFallbackTimeout(): void {
|
|
||||||
if (this.mobileFallbackTimeout) {
|
|
||||||
clearTimeout(this.mobileFallbackTimeout);
|
|
||||||
this.mobileFallbackTimeout = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private isMobileBrowser(): boolean {
|
|
||||||
if (typeof navigator === 'undefined') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const userAgent = navigator.userAgent || navigator.vendor;
|
|
||||||
const isTouchMac = navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1;
|
|
||||||
|
|
||||||
return /Android|iPhone|iPad|iPod|IEMobile|Opera Mini/i.test(userAgent) || isTouchMac;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,12 +88,6 @@ export class AuthService {
|
|||||||
return `https://t.me/${botUsername}?start=${encodeURIComponent(webSessionID)}`;
|
return `https://t.me/${botUsername}?start=${encodeURIComponent(webSessionID)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Generate the Telegram app deep link for mobile auth handoff. */
|
|
||||||
getTelegramAppLoginUrl(webSessionID: string): string {
|
|
||||||
const botUsername = this.getTelegramBotUsername();
|
|
||||||
return `tg://resolve?domain=${encodeURIComponent(botUsername)}&start=${encodeURIComponent(webSessionID)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Get QR code data URL for Telegram login */
|
/** Get QR code data URL for Telegram login */
|
||||||
getTelegramQrUrl(): string {
|
getTelegramQrUrl(): string {
|
||||||
return this.getTelegramLoginUrl();
|
return this.getTelegramLoginUrl();
|
||||||
|
|||||||
Reference in New Issue
Block a user