This repository was archived by the owner on Jul 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLoginButton.tsx
More file actions
63 lines (53 loc) · 2 KB
/
Copy pathLoginButton.tsx
File metadata and controls
63 lines (53 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React, { useEffect, useRef } from 'react';
import { LoginButtonProps, TTelegramAuthLogin } from './types';
import { createScript } from './createScript';
/**
* It takes an object with a bunch of properties and assigns it to the global variable
* `TelegramAuthLogin`
*
* @param {TTelegramAuthLogin} options - The options to set on the global variable.
*/
function initTelegramAuthLogin(options: TTelegramAuthLogin) {
window.TelegramAuthLogin = options;
}
/**
* A React component that renders a Telegram login button.
*
* @deprecated Unmaintained. Deprecated in favour of Telegram's OpenID Connect login. Replace
* this with your own button that starts a standard OIDC redirect — see
* https://github.com/manzoorwanijk/telegram-auth/blob/main/MIGRATION.md
*
* @see https://core.telegram.org/bots/telegram-login
* @see https://core.telegram.org/widgets/login
*
* @param {LoginButtonProps} props The props to pass to the component.
* @returns A React component that renders the Telegram login button.
*/
export function LoginButton(props: LoginButtonProps) {
const hiddenDivRef = useRef<HTMLDivElement>(null);
const scriptRef = useRef<HTMLScriptElement>();
useEffect(() => {
// destroy the existing script element
scriptRef.current?.remove();
// init the global variable
initTelegramAuthLogin({ onAuthCallback: props.onAuthCallback });
// create a new script element and save it
scriptRef.current = createScript(props);
// add the script element to the DOM
hiddenDivRef.current?.after(scriptRef.current);
// Save siblings before unmount
const siblings = hiddenDivRef.current?.parentElement?.children || [];
return () => {
// destroy the script element on unmount
scriptRef.current?.remove();
// We also need to remove the rendered iframe
for (const element of siblings) {
if (element instanceof HTMLIFrameElement && element.src.includes('oauth.telegram.org')) {
element.remove();
break;
}
}
};
}, [props]);
return <div ref={hiddenDivRef} hidden />;
}