Skip to content

Commit

Permalink
🐛 fix: telegram login error handling (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustin01 authored Jul 30, 2024
1 parent 5f8f36d commit cc60ab5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
8 changes: 7 additions & 1 deletion apps/wallet/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RUNTIME_ENV, RUNTIME_PARAMS_RAW } from './utils/runtime';
import { RuntimeEnv } from './utils/basicEnums';
import { AuthenticatorType } from '@deland-labs/hibit-id-sdk';
import authManager from './utils/auth';
import toaster from './components/Toaster';

const MainPage = lazy(() => import('./pages/main'));
const LoginPage = lazy(() => import('./pages/login'));
Expand All @@ -33,7 +34,12 @@ const App: FC = observer(() => {
} else {
// login on launch if is as Telegram Mini App
if (RUNTIME_ENV === RuntimeEnv.TELEGRAM_MINI_APP && RUNTIME_PARAMS_RAW) {
await authManager.login(AuthenticatorType.Telegram, RUNTIME_PARAMS_RAW)
try {
await authManager.login(AuthenticatorType.Telegram, RUNTIME_PARAMS_RAW)
} catch (e) {
console.error(e)
toaster.error('Telegram login failed')
}
}
}
setReady(true)
Expand Down
10 changes: 8 additions & 2 deletions apps/wallet/src/components/AuthenticatorButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AuthenticatorType } from "@deland-labs/hibit-id-sdk";
import { observer } from "mobx-react";
import authManager from "../utils/auth";
import AuthenticatorLogo from "./AuthenticatorLogo";
import toaster from "./Toaster";

export interface AuthenticatorButtonProps {
type: AuthenticatorType
Expand All @@ -11,8 +12,13 @@ export interface AuthenticatorButtonProps {

const AuthenticatorButton: FC<AuthenticatorButtonProps> = observer(({ type, onSuccess }) => {
const handleAuth = async () => {
await authManager.login(type)
onSuccess?.()
try {
await authManager.login(type)
onSuccess?.()
} catch (e) {
console.error(e)
toaster.error(e instanceof Error ? e.message : `${AuthenticatorType[type]} login failed`)
}
}

return (
Expand Down
30 changes: 17 additions & 13 deletions apps/wallet/src/utils/auth/providers/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ export class TelegramAuthenticateProvider implements IAuthenticateProvider {
}

// web login
window.Telegram.Login.auth(
{ bot_id: BOT_ID, request_access: 'write' },
(data: ResponseType) => {
if (!data) {
console.log('ERROR: something went wrong');
}
const userData: any = { ...data }
delete userData.hash
delete userData.auth_date
const queryValue = `user=${encodeURIComponent(JSON.stringify(userData))}&auth_date=${data.auth_date}&hash=${data.hash}`
window.location.href = `${AUTH_SERVER_URL}?tgWebAppData=${encodeURIComponent(queryValue)}`
},
);
return new Promise((resolve, reject) => {
window.Telegram.Login.auth(
{ bot_id: BOT_ID, request_access: 'write' },
(data: ResponseType) => {
if (!data) {
reject(new Error('Telegram login failed'))
return
}
const userData: any = { ...data }
delete userData.hash
delete userData.auth_date
const queryValue = `user=${encodeURIComponent(JSON.stringify(userData))}&auth_date=${data.auth_date}&hash=${data.hash}`
window.location.href = `${AUTH_SERVER_URL}?tgWebAppData=${encodeURIComponent(queryValue)}`
resolve(true)
},
);
})
}
}

0 comments on commit cc60ab5

Please sign in to comment.