Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement seamless telegram login #843

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion packages/@magic-ext/oauth2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import {
OAuthPopupConfiguration,
} from './types';

declare global {
interface Window {
Telegram?: {
WebApp: {
initData: string
};
};
}
}

export class OAuthExtension extends Extension.Internal<'oauth2'> {
name = 'oauth2' as const;
config = {};
Expand All @@ -19,6 +29,11 @@ export class OAuthExtension extends Extension.Internal<'oauth2'> {
'@magic-sdk/react-native-expo': false,
};

constructor() {
super();
this.seamlessTelegramLogin();
}

public loginWithRedirect(configuration: OAuthRedirectConfiguration) {
return this.utils.createPromiEvent<void>(async (resolve, reject) => {
const parseRedirectResult = this.utils.createJsonRpcRequestPayload(OAuthPayloadMethods.Start, [
Expand All @@ -45,7 +60,7 @@ export class OAuthExtension extends Extension.Internal<'oauth2'> {
if (successResult?.oauthAuthoriationURI) {
window.location.href = successResult.useMagicServerCallback
? // @ts-ignore - this.sdk.endpoint is marked protected but we need to access it.
new URL(successResult.oauthAuthoriationURI, this.sdk.endpoint).href
new URL(successResult.oauthAuthoriationURI, this.sdk.endpoint).href
: successResult.oauthAuthoriationURI;
}

Expand Down Expand Up @@ -75,6 +90,33 @@ export class OAuthExtension extends Extension.Internal<'oauth2'> {

return this.request<OAuthRedirectResult | OAuthRedirectError>(requestPayload);
}

protected seamlessTelegramLogin() {
try {
const hash = window.location.hash.toString();
if (!hash.includes('#tgWebAppData')) return;

const script = document.createElement('script');
script.src = 'https://telegram.org/js/telegram-web-app.js';
document.head.prepend(script);

script.onload = async () => {
try {
const userData = window.Telegram?.WebApp.initData;
const requestPayload = this.utils.createJsonRpcRequestPayload(
OAuthPayloadMethods.VerifyTelegramData,
[{ userData, isMiniApp: true }],
);

await this.request<string | null>(requestPayload);
} catch (verificationError) {
console.log('Error while verifying telegram data', verificationError);
}
}
} catch (seamlessLoginError) {
console.log('Error while loading telegram-web-app script', seamlessLoginError);
}
}
}

function getResult(this: OAuthExtension, queryString: string, lifespan?: number) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@magic-ext/oauth2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum OAuthPayloadMethods {
Start = 'magic_oauth_login_with_redirect_start',
Verify = 'magic_oauth_login_with_redirect_verify',
Popup = 'magic_oauth_login_with_popup',
VerifyTelegramData = 'magic_oauth_verify_telegram_data',
}

export type OAuthProvider =
Expand Down Expand Up @@ -101,7 +102,7 @@ export interface OAuthRedirectConfiguration {
}

export interface OAuthPopupConfiguration {
provider: OAuthProvider;
Ethella marked this conversation as resolved.
Show resolved Hide resolved
provider: OAuthProvider | 'telegram';
scope?: string[];
loginHint?: string;
}
Expand Down