I am developing a Telegram mini-app that allows users to refer others and earn rewards. Although I have successfully implemented authentication and several functionalities, I am struggling to extract the referral code from the referral link, such as ‘Telegram: Contact @my_bot’. I’ve attempted various methods without success. Could someone please assist me in resolving this? Thank you! Below is my JavaScript code:
const telegramApi = await initializeTelegramSdk();
if (telegramApi) {
const { id, firstName, userHandle, startParameter } = telegramApi.initDataUnsafe;
console.log('User Information from Telegram:', { id, firstName, userHandle, startParameter });
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content');
const apiHeaders = {
'Content-Type': 'application/json',
};
if (csrfToken) {
apiHeaders['X-CSRF-TOKEN'] = csrfToken;
}
const apiRequestBody = {
...telegramApi.initDataUnsafe,
referralCode: startParameter // Adding the referral code to the request body
};
const apiResponse = await fetch("{{ route('tg.authenticate') }}", {
method: 'POST',
headers: apiHeaders,
body: JSON.stringify(apiRequestBody) // Sending the referral code in the payload
});
const apiData = await apiResponse.json();
if (apiData.message === 'Logged in successfully' || apiData.message === 'User created and logged in') {
console.log(`Hello ${firstName || userHandle}!`);
setTimeout(() => {
window.location.href = '{{ route('tg.dashboard') }}'; // Redirecting after 1.5 seconds
}, 1500);
} else {
console.error('Login unsuccessful:', apiData.message);
}
}
In my Laravel controller, I utilized Log::info('Request Input:', $request->all());
to verify the input, but the JavaScript is not transmitting the referral code.