@sneat/auth-ui 0.27.30 → 0.27.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { DOCUMENT } from '@angular/common';
|
|
2
2
|
import { ChangeDetectionStrategy, Component, DestroyRef, ElementRef, inject, input, signal, } from '@angular/core';
|
|
3
|
+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
3
4
|
import { ErrorLogger } from '@sneat/core';
|
|
4
5
|
import { SneatAuthWithTelegramService, } from './sneat-auth-with-telegram.service';
|
|
5
6
|
import { TelegramLoginConfig, resolveTelegramBotID, } from './telegram-login-config';
|
|
6
7
|
import { loadTelegramLoginScript, telegramLoginApi, } from './telegram-login-js';
|
|
7
8
|
import * as i0 from "@angular/core";
|
|
8
|
-
let authWithTelegramService;
|
|
9
9
|
/** Server nonces live 10 minutes; refresh well inside that. */
|
|
10
10
|
const OIDC_NONCE_REFRESH_MS = 8 * 60 * 1000;
|
|
11
11
|
/**
|
|
@@ -14,7 +14,6 @@ const OIDC_NONCE_REFRESH_MS = 8 * 60 * 1000;
|
|
|
14
14
|
* Widget (telegram-widget.js), e.g. the local dev bot.
|
|
15
15
|
*/
|
|
16
16
|
export class LoginWithTelegramComponent {
|
|
17
|
-
// TODO: Article about Telegram login
|
|
18
17
|
constructor() {
|
|
19
18
|
this.el = inject(ElementRef);
|
|
20
19
|
this.document = inject(DOCUMENT);
|
|
@@ -37,21 +36,31 @@ export class LoginWithTelegramComponent {
|
|
|
37
36
|
/** Set once the OpenID popup can be offered: Client ID plus a live nonce. */
|
|
38
37
|
this.oidc = signal(undefined, /* @ts-ignore */
|
|
39
38
|
...(ngDevMode ? [{ debugName: "oidc" }] : /* istanbul ignore next */ []));
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
// Register while the view is alive, never from a late loader continuation.
|
|
40
|
+
this.destroyRef.onDestroy(() => {
|
|
41
|
+
clearInterval(this.nonceTimer);
|
|
42
|
+
const host = this.document.defaultView;
|
|
43
|
+
if (host && host.onTelegramAuth === this.legacyCallback)
|
|
44
|
+
delete host.onTelegramAuth;
|
|
45
|
+
});
|
|
42
46
|
}
|
|
43
47
|
ngOnInit() {
|
|
44
48
|
const botID = this.botID();
|
|
45
49
|
if (!botID) {
|
|
46
50
|
return;
|
|
47
51
|
}
|
|
48
|
-
this.authWithTelegram
|
|
52
|
+
this.authWithTelegram
|
|
53
|
+
.getTelegramOIDCNonce(botID)
|
|
54
|
+
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
55
|
+
.subscribe({
|
|
49
56
|
next: (cfg) => this.startOIDC(botID, cfg),
|
|
50
57
|
// The server answers 400 for a bot without OpenID login configured.
|
|
51
58
|
error: () => this.startLegacyWidget(botID),
|
|
52
59
|
});
|
|
53
60
|
}
|
|
54
61
|
loginWithOIDC(cfg) {
|
|
62
|
+
if (this.destroyRef.destroyed)
|
|
63
|
+
return;
|
|
55
64
|
const api = telegramLoginApi(this.document.defaultView ?? window);
|
|
56
65
|
if (!api) {
|
|
57
66
|
this.errorLogger.logErrorHandler('Telegram login library not loaded')(new Error('window.Telegram.Login is undefined'));
|
|
@@ -65,7 +74,7 @@ export class LoginWithTelegramComponent {
|
|
|
65
74
|
nonce: cfg.nonce,
|
|
66
75
|
request_access: this.requestAccess() === 'write' ? ['write'] : [],
|
|
67
76
|
}, (result) => {
|
|
68
|
-
if (result.id_token) {
|
|
77
|
+
if (result.id_token && !this.destroyRef.destroyed) {
|
|
69
78
|
this.authWithTelegram.loginWithTelegramOIDC(botID, result.id_token, this.isUserAuthenticated());
|
|
70
79
|
this.refreshNonce(botID);
|
|
71
80
|
}
|
|
@@ -75,27 +84,38 @@ export class LoginWithTelegramComponent {
|
|
|
75
84
|
}
|
|
76
85
|
startOIDC(botID, cfg) {
|
|
77
86
|
loadTelegramLoginScript(this.document).then(() => {
|
|
87
|
+
if (this.destroyRef.destroyed)
|
|
88
|
+
return;
|
|
78
89
|
this.oidc.set(cfg);
|
|
79
|
-
|
|
80
|
-
this.
|
|
90
|
+
clearInterval(this.nonceTimer);
|
|
91
|
+
this.nonceTimer = setInterval(() => this.refreshNonce(botID), OIDC_NONCE_REFRESH_MS);
|
|
81
92
|
}, this.errorLogger.logErrorHandler('Failed to load Telegram login library'));
|
|
82
93
|
}
|
|
83
94
|
refreshNonce(botID) {
|
|
84
|
-
this.authWithTelegram
|
|
95
|
+
this.authWithTelegram
|
|
96
|
+
.getTelegramOIDCNonce(botID)
|
|
97
|
+
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
98
|
+
.subscribe({
|
|
85
99
|
next: (cfg) => this.oidc.set(cfg),
|
|
86
100
|
error: this.errorLogger.logErrorHandler('Failed to refresh Telegram login nonce'),
|
|
87
101
|
});
|
|
88
102
|
}
|
|
89
103
|
startLegacyWidget(botID) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
104
|
+
if (this.destroyRef.destroyed)
|
|
105
|
+
return;
|
|
106
|
+
this.legacyCallback = (tgAuthData) => {
|
|
107
|
+
if (this.destroyRef.destroyed)
|
|
108
|
+
return;
|
|
93
109
|
// https://core.telegram.org/widgets/login-legacy
|
|
94
110
|
// After a successful authorization, the widget returns data
|
|
95
111
|
// by calling the callback function data-onauth with the JSON-object containing
|
|
96
112
|
// id, first_name, last_name, username, photo_url, auth_date and hash fields.
|
|
97
|
-
|
|
113
|
+
this.authWithTelegram.loginWithTelegram(botID, tgAuthData, this.isUserAuthenticated());
|
|
98
114
|
};
|
|
115
|
+
const host = this.document.defaultView;
|
|
116
|
+
if (!host)
|
|
117
|
+
return;
|
|
118
|
+
host.onTelegramAuth = this.legacyCallback;
|
|
99
119
|
const script = this.document.createElement('script');
|
|
100
120
|
script.src = 'https://telegram.org/js/telegram-widget.js?22';
|
|
101
121
|
script.setAttribute('data-telegram-login', botID);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"login-with-telegram.component.js","sourceRoot":"","sources":["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E;YACxD,iDAAiD;YACjD,4DAA4D;YAC5D,+EAA+E;YAC/E,6EAA6E;YAC7E,uBAAuB,CAAC,iBAAiB,CACvC,KAAK,EACL,UAAU,EACV,IAAI,CAAC,mBAAmB,EAAE,CAC3B,CAAC;QACJ,CAAC,CAAC;QAEF,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAErD,MAAM,CAAC,GAAG,GAAG,+CAA+C,CAAC;QAC7D,MAAM,CAAC,YAAY,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAC;QAClD,MAAM,CAAC,YAAY,CAAC,qBAAqB,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC;QACjE,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;YACpB,MAAM,CAAC,YAAY,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;QAC/C,CAAC;QACD,MAAM,CAAC,YAAY,CAAC,aAAa,EAAE,sBAAsB,CAAC,CAAC;QAC3D,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;8GAtHU,0BAA0B;kGAA1B,0BAA0B,ivBAtC1B,CAAC,4BAA4B,CAAC,0BAG/B;;;;;;;;;;;GAWT;;2FAwBU,0BAA0B;kBAvCtC,SAAS;gCACG,CAAC,4BAA4B,CAAC,YAC/B,2BAA2B,mBACpB,uBAAuB,CAAC,MAAM,YACrC;;;;;;;;;;;GAWT","sourcesContent":["import { DOCUMENT } from '@angular/common';\nimport {\n ChangeDetectionStrategy,\n Component,\n DestroyRef,\n ElementRef,\n OnInit,\n inject,\n input,\n signal,\n} from '@angular/core';\nimport { ErrorLogger, IErrorLogger } from '@sneat/core';\nimport {\n ITelegramAuthData,\n ITelegramOIDCNonce,\n SneatAuthWithTelegramService,\n} from './sneat-auth-with-telegram.service';\nimport {\n TelegramLoginConfig,\n resolveTelegramBotID,\n} from './telegram-login-config';\nimport {\n ITelegramLoginResult,\n loadTelegramLoginScript,\n telegramLoginApi,\n} from './telegram-login-js';\n\nlet authWithTelegramService: SneatAuthWithTelegramService;\n\n/** Server nonces live 10 minutes; refresh well inside that. */\nconst OIDC_NONCE_REFRESH_MS = 8 * 60 * 1000;\n\n/**\n * Telegram login button. Bots with OpenID login configured server-side use\n * Telegram's OpenID popup (telegram-login.js); others keep the legacy Login\n * Widget (telegram-widget.js), e.g. the local dev bot.\n */\n@Component({\n providers: [SneatAuthWithTelegramService],\n selector: 'sneat-login-with-telegram',\n changeDetection: ChangeDetectionStrategy.OnPush,\n template: `\n @if (oidc(); as cfg) {\n <button\n type=\"button\"\n class=\"tg-oidc-login\"\n [attr.data-size]=\"size()\"\n (click)=\"loginWithOIDC(cfg)\"\n >\n Log in with Telegram\n </button>\n }\n `,\n styles: [\n `\n .tg-oidc-login {\n background: #54a9eb;\n color: #fff;\n border: 0;\n border-radius: 20px;\n font-weight: 500;\n cursor: pointer;\n font-size: 16px;\n padding: 10px 21px;\n }\n .tg-oidc-login[data-size='medium'] {\n font-size: 14px;\n padding: 8px 16px;\n }\n .tg-oidc-login[data-size='small'] {\n font-size: 13px;\n padding: 6px 12px;\n }\n `,\n ],\n})\nexport class LoginWithTelegramComponent implements OnInit {\n private readonly el = inject(ElementRef);\n private readonly document = inject<Document>(DOCUMENT);\n private readonly destroyRef = inject(DestroyRef);\n private readonly errorLogger = inject<IErrorLogger>(ErrorLogger);\n readonly authWithTelegram = inject(SneatAuthWithTelegramService);\n private readonly telegramLoginConfig = inject(TelegramLoginConfig, {\n optional: true,\n });\n\n // TODO: Article about Telegram login\n constructor() {\n const authWithTelegram = this.authWithTelegram;\n\n authWithTelegramService = authWithTelegram;\n }\n\n public readonly isUserAuthenticated = input(false);\n\n public readonly botID = input<string>(resolveTelegramBotID(this.telegramLoginConfig, location.hostname));\n\n public readonly size = input<'small' | 'medium' | 'large'>('large');\n public readonly requestAccess = input<'write' | 'read'>('write');\n public readonly userPic = input(true);\n\n /** Set once the OpenID popup can be offered: Client ID plus a live nonce. */\n protected readonly oidc = signal<ITelegramOIDCNonce | undefined>(undefined);\n\n ngOnInit() {\n const botID = this.botID();\n if (!botID) {\n return;\n }\n this.authWithTelegram.getTelegramOIDCNonce(botID).subscribe({\n next: (cfg) => this.startOIDC(botID, cfg),\n // The server answers 400 for a bot without OpenID login configured.\n error: () => this.startLegacyWidget(botID),\n });\n }\n\n protected loginWithOIDC(cfg: ITelegramOIDCNonce): void {\n const api = telegramLoginApi(this.document.defaultView ?? window);\n if (!api) {\n this.errorLogger.logErrorHandler('Telegram login library not loaded')(\n new Error('window.Telegram.Login is undefined'),\n );\n return;\n }\n const botID = this.botID();\n // Called straight from the click handler: the library opens its popup\n // synchronously, which keeps the browser's popup blocker out of the way.\n api.auth(\n {\n client_id: Number(cfg.clientID),\n nonce: cfg.nonce,\n request_access: this.requestAccess() === 'write' ? ['write'] : [],\n },\n (result: ITelegramLoginResult) => {\n if (result.id_token) {\n this.authWithTelegram.loginWithTelegramOIDC(\n botID,\n result.id_token,\n this.isUserAuthenticated(),\n );\n this.refreshNonce(botID);\n }\n // result.error (e.g. the user closed the popup) needs no action:\n // the button stays, and the user can try again.\n },\n );\n }\n\n private startOIDC(botID: string, cfg: ITelegramOIDCNonce): void {\n loadTelegramLoginScript(this.document).then(() => {\n this.oidc.set(cfg);\n const timer = setInterval(\n () => this.refreshNonce(botID),\n OIDC_NONCE_REFRESH_MS,\n );\n this.destroyRef.onDestroy(() => clearInterval(timer));\n }, this.errorLogger.logErrorHandler('Failed to load Telegram login library'));\n }\n\n private refreshNonce(botID: string): void {\n this.authWithTelegram.getTelegramOIDCNonce(botID).subscribe({\n next: (cfg) => this.oidc.set(cfg),\n error: this.errorLogger.logErrorHandler(\n 'Failed to refresh Telegram login nonce',\n ),\n });\n }\n\n private startLegacyWidget(botID: string): void {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n window.onTelegramAuth = (tgAuthData: ITelegramAuthData) => {\n // https://core.telegram.org/widgets/login-legacy\n // After a successful authorization, the widget returns data\n // by calling the callback function data-onauth with the JSON-object containing\n // id, first_name, last_name, username, photo_url, auth_date and hash fields.\n authWithTelegramService.loginWithTelegram(\n botID,\n tgAuthData,\n this.isUserAuthenticated(),\n );\n };\n\n const script = this.document.createElement('script');\n\n script.src = 'https://telegram.org/js/telegram-widget.js?22';\n script.setAttribute('data-telegram-login', botID);\n script.setAttribute('data-request-access', this.requestAccess());\n script.setAttribute('data-size', this.size());\n if (!this.userPic()) {\n script.setAttribute('data-userpic', 'false');\n }\n script.setAttribute('data-onauth', 'onTelegramAuth(user)');\n this.el.nativeElement.appendChild(script);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"login-with-telegram.component.js","sourceRoot":"","sources":["../../../../../../../../libs/auth/ui/src/lib/pages/login-page/login-with-telegram.component.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EACL,uBAAuB,EACvB,SAAS,EACT,UAAU,EACV,UAAU,EAEV,MAAM,EACN,KAAK,EACL,MAAM,GACP,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAgB,MAAM,aAAa,CAAC;AACxD,OAAO,EAGL,4BAA4B,GAC7B,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EACL,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAEL,uBAAuB,EACvB,gBAAgB,GACjB,MAAM,qBAAqB,CAAC;;AAE7B,+DAA+D;AAC/D,MAAM,qBAAqB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAE5C;;;;GAIG;AAwCH,MAAM,OAAO,0BAA0B;IAarC;QA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ACrC,KAAK,EACL,UAAU,EACV,IAAI,CAAC,mBAAmB,EAAE,CAC3B,CAAC;QACJ,CAAC,CAAC;QAEF,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,WAEnB,CAAC;QACT,IAAI,CAAC,IAAI;YAAE,OAAO;QAClB,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc,CAAC;QAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAErD,MAAM,CAAC,GAAG,GAAG,+CAA+C,CAAC;QAC7D,MAAM,CAAC,YAAY,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAC;QAClD,MAAM,CAAC,YAAY,CAAC,qBAAqB,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC;QACjE,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;YACpB,MAAM,CAAC,YAAY,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;QAC/C,CAAC;QACD,MAAM,CAAC,YAAY,CAAC,aAAa,EAAE,sBAAsB,CAAC,CAAC;QAC3D,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;8GA7IU,0BAA0B;kGAA1B,0BAA0B,ivBAtC1B,CAAC,4BAA4B,CAAC,0BAG/B;;;;;;;;;;;GAWT;;2FAwBU,0BAA0B;kBAvCtC,SAAS;gCACG,CAAC,4BAA4B,CAAC,YAC/B,2BAA2B,mBACpB,uBAAuB,CAAC,MAAM,YACrC;;;;;;;;;;;GAWT","sourcesContent":["import { DOCUMENT } from '@angular/common';\nimport {\n ChangeDetectionStrategy,\n Component,\n DestroyRef,\n ElementRef,\n OnInit,\n inject,\n input,\n signal,\n} from '@angular/core';\nimport { takeUntilDestroyed } from '@angular/core/rxjs-interop';\nimport { ErrorLogger, IErrorLogger } from '@sneat/core';\nimport {\n ITelegramAuthData,\n ITelegramOIDCNonce,\n SneatAuthWithTelegramService,\n} from './sneat-auth-with-telegram.service';\nimport {\n TelegramLoginConfig,\n resolveTelegramBotID,\n} from './telegram-login-config';\nimport {\n ITelegramLoginResult,\n loadTelegramLoginScript,\n telegramLoginApi,\n} from './telegram-login-js';\n\n/** Server nonces live 10 minutes; refresh well inside that. */\nconst OIDC_NONCE_REFRESH_MS = 8 * 60 * 1000;\n\n/**\n * Telegram login button. Bots with OpenID login configured server-side use\n * Telegram's OpenID popup (telegram-login.js); others keep the legacy Login\n * Widget (telegram-widget.js), e.g. the local dev bot.\n */\n@Component({\n providers: [SneatAuthWithTelegramService],\n selector: 'sneat-login-with-telegram',\n changeDetection: ChangeDetectionStrategy.OnPush,\n template: `\n @if (oidc(); as cfg) {\n <button\n type=\"button\"\n class=\"tg-oidc-login\"\n [attr.data-size]=\"size()\"\n (click)=\"loginWithOIDC(cfg)\"\n >\n Log in with Telegram\n </button>\n }\n `,\n styles: [\n `\n .tg-oidc-login {\n background: #54a9eb;\n color: #fff;\n border: 0;\n border-radius: 20px;\n font-weight: 500;\n cursor: pointer;\n font-size: 16px;\n padding: 10px 21px;\n }\n .tg-oidc-login[data-size='medium'] {\n font-size: 14px;\n padding: 8px 16px;\n }\n .tg-oidc-login[data-size='small'] {\n font-size: 13px;\n padding: 6px 12px;\n }\n `,\n ],\n})\nexport class LoginWithTelegramComponent implements OnInit {\n private readonly el = inject(ElementRef);\n private readonly document = inject<Document>(DOCUMENT);\n private readonly destroyRef = inject(DestroyRef);\n private readonly errorLogger = inject<IErrorLogger>(ErrorLogger);\n readonly authWithTelegram = inject(SneatAuthWithTelegramService);\n private readonly telegramLoginConfig = inject(TelegramLoginConfig, {\n optional: true,\n });\n\n private nonceTimer?: ReturnType<typeof setInterval>;\n private legacyCallback?: (data: ITelegramAuthData) => void;\n\n constructor() {\n // Register while the view is alive, never from a late loader continuation.\n this.destroyRef.onDestroy(() => {\n clearInterval(this.nonceTimer);\n const host = this.document.defaultView as\n | (Window & { onTelegramAuth?: (data: ITelegramAuthData) => void })\n | null;\n if (host && host.onTelegramAuth === this.legacyCallback)\n delete host.onTelegramAuth;\n });\n }\n\n public readonly isUserAuthenticated = input(false);\n\n public readonly botID = input<string>(\n resolveTelegramBotID(this.telegramLoginConfig, location.hostname),\n );\n\n public readonly size = input<'small' | 'medium' | 'large'>('large');\n public readonly requestAccess = input<'write' | 'read'>('write');\n public readonly userPic = input(true);\n\n /** Set once the OpenID popup can be offered: Client ID plus a live nonce. */\n protected readonly oidc = signal<ITelegramOIDCNonce | undefined>(undefined);\n\n ngOnInit() {\n const botID = this.botID();\n if (!botID) {\n return;\n }\n this.authWithTelegram\n .getTelegramOIDCNonce(botID)\n .pipe(takeUntilDestroyed(this.destroyRef))\n .subscribe({\n next: (cfg) => this.startOIDC(botID, cfg),\n // The server answers 400 for a bot without OpenID login configured.\n error: () => this.startLegacyWidget(botID),\n });\n }\n\n protected loginWithOIDC(cfg: ITelegramOIDCNonce): void {\n if (this.destroyRef.destroyed) return;\n const api = telegramLoginApi(this.document.defaultView ?? window);\n if (!api) {\n this.errorLogger.logErrorHandler('Telegram login library not loaded')(\n new Error('window.Telegram.Login is undefined'),\n );\n return;\n }\n const botID = this.botID();\n // Called straight from the click handler: the library opens its popup\n // synchronously, which keeps the browser's popup blocker out of the way.\n api.auth(\n {\n client_id: Number(cfg.clientID),\n nonce: cfg.nonce,\n request_access: this.requestAccess() === 'write' ? ['write'] : [],\n },\n (result: ITelegramLoginResult) => {\n if (result.id_token && !this.destroyRef.destroyed) {\n this.authWithTelegram.loginWithTelegramOIDC(\n botID,\n result.id_token,\n this.isUserAuthenticated(),\n );\n this.refreshNonce(botID);\n }\n // result.error (e.g. the user closed the popup) needs no action:\n // the button stays, and the user can try again.\n },\n );\n }\n\n private startOIDC(botID: string, cfg: ITelegramOIDCNonce): void {\n loadTelegramLoginScript(this.document).then(() => {\n if (this.destroyRef.destroyed) return;\n this.oidc.set(cfg);\n clearInterval(this.nonceTimer);\n this.nonceTimer = setInterval(\n () => this.refreshNonce(botID),\n OIDC_NONCE_REFRESH_MS,\n );\n }, this.errorLogger.logErrorHandler('Failed to load Telegram login library'));\n }\n\n private refreshNonce(botID: string): void {\n this.authWithTelegram\n .getTelegramOIDCNonce(botID)\n .pipe(takeUntilDestroyed(this.destroyRef))\n .subscribe({\n next: (cfg) => this.oidc.set(cfg),\n error: this.errorLogger.logErrorHandler(\n 'Failed to refresh Telegram login nonce',\n ),\n });\n }\n\n private startLegacyWidget(botID: string): void {\n if (this.destroyRef.destroyed) return;\n this.legacyCallback = (tgAuthData: ITelegramAuthData) => {\n if (this.destroyRef.destroyed) return;\n // https://core.telegram.org/widgets/login-legacy\n // After a successful authorization, the widget returns data\n // by calling the callback function data-onauth with the JSON-object containing\n // id, first_name, last_name, username, photo_url, auth_date and hash fields.\n this.authWithTelegram.loginWithTelegram(\n botID,\n tgAuthData,\n this.isUserAuthenticated(),\n );\n };\n\n const host = this.document.defaultView as\n | (Window & { onTelegramAuth?: (data: ITelegramAuthData) => void })\n | null;\n if (!host) return;\n host.onTelegramAuth = this.legacyCallback;\n const script = this.document.createElement('script');\n\n script.src = 'https://telegram.org/js/telegram-widget.js?22';\n script.setAttribute('data-telegram-login', botID);\n script.setAttribute('data-request-access', this.requestAccess());\n script.setAttribute('data-size', this.size());\n if (!this.userPic()) {\n script.setAttribute('data-userpic', 'false');\n }\n script.setAttribute('data-onauth', 'onTelegramAuth(user)');\n this.el.nativeElement.appendChild(script);\n }\n}\n"]}
|
|
@@ -13,6 +13,8 @@ export declare class LoginWithTelegramComponent implements OnInit {
|
|
|
13
13
|
private readonly errorLogger;
|
|
14
14
|
readonly authWithTelegram: SneatAuthWithTelegramService;
|
|
15
15
|
private readonly telegramLoginConfig;
|
|
16
|
+
private nonceTimer?;
|
|
17
|
+
private legacyCallback?;
|
|
16
18
|
constructor();
|
|
17
19
|
readonly isUserAuthenticated: import("@angular/core").InputSignal<boolean>;
|
|
18
20
|
readonly botID: import("@angular/core").InputSignal<string>;
|