The following code snippet demonstrates how to implement login via Telegram to create a Metapro ID:
export const useInitialFetch = () => {
// Get user data from the Redux store
const userData = useAppSelector((state) => state.user)
// Initialize the Telegram login mutation
const [telegramLogin] = useTelegramLoginMutation()
// Get the router object for navigation
const { push } = useRouter()
// Function to check Telegram WebApp and perform login if necessary
const checkTelegramWebApp = async () => {
// Check if running in a browser environment and Telegram WebApp is available
if (typeof window !== 'undefined' && Telegram?.WebApp) {
const initDataUnsafe = Telegram.WebApp?.initDataUnsafe
const telegramUser = initDataUnsafe?.user
const refCode = initDataUnsafe?.start_param
// If Telegram user exists and no user data in the store, perform login
if (telegramUser && !userData?.user) {
await telegramLogin({
id: telegramUser.id,
first_name: telegramUser.first_name,
last_name: telegramUser.last_name,
username: telegramUser.username,
telegramSimpleString: Telegram.WebApp?.initData,
...(refCode && { referralCode: refCode })
}).unwrap()
push(AppPath.LOGIN)
}
} else {
// Retry checking Telegram WebApp after 100ms if not available
setTimeout(checkTelegramWebApp, 100)
}
}
// useEffect to run the checkTelegramWebApp function on component mount
useEffect(() => {
checkTelegramWebApp()
}, [])
}
Based on this example, the login via Telegram occurs in the background when the Mini App is launched. This code sends the user's Telegram data to the backend, which authenticates the user and creates a Metapro ID, returning an access token.
Adding a Wallet to Metapro ID
Once the user has logged in via Telegram, they can connect their Metapro Wallet to their Metapro ID: