telegram desktop
This commit is contained in:
@@ -23,6 +23,7 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
|
|
||||||
private readonly pollIntervalMs = 5000;
|
private readonly pollIntervalMs = 5000;
|
||||||
private pollTimer?: ReturnType<typeof setInterval>;
|
private pollTimer?: ReturnType<typeof setInterval>;
|
||||||
|
private telegramFallbackTimer?: ReturnType<typeof setTimeout>;
|
||||||
private readonly handleVisibilityChange = () => {
|
private readonly handleVisibilityChange = () => {
|
||||||
if (typeof document !== 'undefined' && document.visibilityState === 'visible') {
|
if (typeof document !== 'undefined' && document.visibilityState === 'visible') {
|
||||||
this.checkLoginAfterReturn();
|
this.checkLoginAfterReturn();
|
||||||
@@ -41,6 +42,7 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
this.initQrLogin();
|
this.initQrLogin();
|
||||||
} else {
|
} else {
|
||||||
this.awaitingTelegramReturn.set(false);
|
this.awaitingTelegramReturn.set(false);
|
||||||
|
this.clearTelegramFallback();
|
||||||
this.stopPolling();
|
this.stopPolling();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -54,6 +56,7 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
|
|
||||||
ngOnDestroy(): void {
|
ngOnDestroy(): void {
|
||||||
this.awaitingTelegramReturn.set(false);
|
this.awaitingTelegramReturn.set(false);
|
||||||
|
this.clearTelegramFallback();
|
||||||
this.stopPolling();
|
this.stopPolling();
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
@@ -65,6 +68,7 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
|
|
||||||
close(): void {
|
close(): void {
|
||||||
this.awaitingTelegramReturn.set(false);
|
this.awaitingTelegramReturn.set(false);
|
||||||
|
this.clearTelegramFallback();
|
||||||
this.authService.hideLogin();
|
this.authService.hideLogin();
|
||||||
this.stopPolling();
|
this.stopPolling();
|
||||||
}
|
}
|
||||||
@@ -78,23 +82,39 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
this.startPolling(webSessionID);
|
this.startPolling(webSessionID);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isMobileBrowser()) {
|
const appUrl = this.authService.getTelegramAppLoginUrl(webSessionID);
|
||||||
this.awaitingTelegramReturn.set(true);
|
this.awaitingTelegramReturn.set(true);
|
||||||
window.location.href = this.authService.getTelegramAppLoginUrl(webSessionID);
|
this.clearTelegramFallback();
|
||||||
|
|
||||||
|
if (this.isMobileBrowser()) {
|
||||||
|
window.location.href = appUrl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.openExternalApp(appUrl);
|
||||||
|
this.telegramFallbackTimer = window.setTimeout(() => {
|
||||||
|
if (!this.showDialog() || !this.awaitingTelegramReturn()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof document !== 'undefined' && (!document.hasFocus() || document.visibilityState === 'hidden')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
window.open(url, '_blank', 'noopener,noreferrer');
|
window.open(url, '_blank', 'noopener,noreferrer');
|
||||||
|
}, 1200);
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshQr(): void {
|
refreshQr(): void {
|
||||||
this.awaitingTelegramReturn.set(false);
|
this.awaitingTelegramReturn.set(false);
|
||||||
|
this.clearTelegramFallback();
|
||||||
this.stopPolling();
|
this.stopPolling();
|
||||||
this.initQrLogin();
|
this.initQrLogin();
|
||||||
}
|
}
|
||||||
|
|
||||||
private initQrLogin(): void {
|
private initQrLogin(): void {
|
||||||
this.awaitingTelegramReturn.set(false);
|
this.awaitingTelegramReturn.set(false);
|
||||||
|
this.clearTelegramFallback();
|
||||||
this.qrStatus.set('loading');
|
this.qrStatus.set('loading');
|
||||||
this.loginUrl.set('');
|
this.loginUrl.set('');
|
||||||
this.webSessionID.set('');
|
this.webSessionID.set('');
|
||||||
@@ -129,6 +149,7 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
next: (session) => {
|
next: (session) => {
|
||||||
if (session?.active) {
|
if (session?.active) {
|
||||||
this.awaitingTelegramReturn.set(false);
|
this.awaitingTelegramReturn.set(false);
|
||||||
|
this.clearTelegramFallback();
|
||||||
this.stopPolling();
|
this.stopPolling();
|
||||||
this.authService.onTelegramLoginComplete();
|
this.authService.onTelegramLoginComplete();
|
||||||
}
|
}
|
||||||
@@ -147,6 +168,13 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private clearTelegramFallback(): void {
|
||||||
|
if (this.telegramFallbackTimer) {
|
||||||
|
clearTimeout(this.telegramFallbackTimer);
|
||||||
|
this.telegramFallbackTimer = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private checkLoginAfterReturn(): void {
|
private checkLoginAfterReturn(): void {
|
||||||
if (!this.showDialog() || !this.awaitingTelegramReturn()) {
|
if (!this.showDialog() || !this.awaitingTelegramReturn()) {
|
||||||
return;
|
return;
|
||||||
@@ -165,12 +193,27 @@ export class TelegramLoginComponent implements OnDestroy {
|
|||||||
this.authService.checkSessionOnce(webSessionID).subscribe(session => {
|
this.authService.checkSessionOnce(webSessionID).subscribe(session => {
|
||||||
if (session?.active) {
|
if (session?.active) {
|
||||||
this.awaitingTelegramReturn.set(false);
|
this.awaitingTelegramReturn.set(false);
|
||||||
|
this.clearTelegramFallback();
|
||||||
this.stopPolling();
|
this.stopPolling();
|
||||||
this.authService.onTelegramLoginComplete();
|
this.authService.onTelegramLoginComplete();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private openExternalApp(url: string): void {
|
||||||
|
if (typeof document === 'undefined') {
|
||||||
|
window.location.href = url;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const anchor = document.createElement('a');
|
||||||
|
anchor.href = url;
|
||||||
|
anchor.style.display = 'none';
|
||||||
|
document.body.appendChild(anchor);
|
||||||
|
anchor.click();
|
||||||
|
anchor.remove();
|
||||||
|
}
|
||||||
|
|
||||||
private isMobileBrowser(): boolean {
|
private isMobileBrowser(): boolean {
|
||||||
if (typeof navigator === 'undefined') {
|
if (typeof navigator === 'undefined') {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user