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

🐛 fix: telegram login error handling #59

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
},
);
})
}
}
Loading