The following code snippet demonstrates how to implement login via Telegram to create a Metapro ID:
exportconstuseInitialFetch= () => {// Get user data from the Redux storeconstuserData=useAppSelector((state) =>state.user)// Initialize the Telegram login mutationconst [telegramLogin] =useTelegramLoginMutation()// Get the router object for navigationconst { push } =useRouter()// Function to check Telegram WebApp and perform login if necessaryconstcheckTelegramWebApp=async () => {// Check if running in a browser environment and Telegram WebApp is availableif (typeof window !=='undefined'&&Telegram?.WebApp) {constinitDataUnsafe=Telegram.WebApp?.initDataUnsafeconsttelegramUser=initDataUnsafe?.userconstrefCode=initDataUnsafe?.start_param// If Telegram user exists and no user data in the store, perform loginif (telegramUser &&!userData?.user) {awaittelegramLogin({ 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 availablesetTimeout(checkTelegramWebApp,100) } }// useEffect to run the checkTelegramWebApp function on component mountuseEffect(() => {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: