mirror of
https://github.com/pezkuwichain/pezkuwi-p2p-mobile.git
synced 2026-10-20 18:56:22 +00:00
fix: login button now supports session_token from URL
This commit is contained in:
1 parent
5236f8c470
commit
0ca56b8fe4
1 file changed
+36
-14
@@ -85,7 +85,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
return tg.initDataUnsafe.user;
|
return tg.initDataUnsafe.user;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Login with Telegram
|
// Login with Telegram or session token
|
||||||
const login = useCallback(async () => {
|
const login = useCallback(async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
@@ -93,29 +93,20 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
try {
|
try {
|
||||||
const tg = window.Telegram?.WebApp;
|
const tg = window.Telegram?.WebApp;
|
||||||
|
|
||||||
if (!tg?.initData) {
|
// Method 1: Telegram WebApp available
|
||||||
throw new Error('Telegram WebApp not available. Open from Telegram.');
|
if (tg?.initData) {
|
||||||
}
|
|
||||||
|
|
||||||
// Call Supabase Edge Function to verify initData and get/create user
|
|
||||||
const { data, error: fnError } = await supabase.functions.invoke('telegram-auth', {
|
const { data, error: fnError } = await supabase.functions.invoke('telegram-auth', {
|
||||||
body: { initData: tg.initData }
|
body: { initData: tg.initData }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (fnError) throw fnError;
|
if (fnError) throw fnError;
|
||||||
|
if (!data?.user) throw new Error('Authentication failed');
|
||||||
if (!data?.user) {
|
|
||||||
throw new Error('Authentication failed');
|
|
||||||
}
|
|
||||||
|
|
||||||
setUser(data.user);
|
setUser(data.user);
|
||||||
// Use auth_user_id for P2P operations (balance queries, etc.)
|
|
||||||
// This is the auth.users ID used by p2p_deposit_withdraw_requests FK
|
|
||||||
const p2pUserId = data.auth_user_id || data.user.id;
|
const p2pUserId = data.auth_user_id || data.user.id;
|
||||||
setCurrentUserId(p2pUserId);
|
setCurrentUserId(p2pUserId);
|
||||||
setTelegramUser(getTelegramUser());
|
setTelegramUser(getTelegramUser());
|
||||||
|
|
||||||
// Store session token and auth_user_id if provided
|
|
||||||
if (data.session_token) {
|
if (data.session_token) {
|
||||||
localStorage.setItem('p2p_session', data.session_token);
|
localStorage.setItem('p2p_session', data.session_token);
|
||||||
}
|
}
|
||||||
@@ -124,10 +115,41 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.Telegram?.WebApp.HapticFeedback.notificationOccurred('success');
|
window.Telegram?.WebApp.HapticFeedback.notificationOccurred('success');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method 2: URL session_token (from MiniApp redirect)
|
||||||
|
const params = new URLSearchParams(window.location.search);
|
||||||
|
const sessionToken = params.get('session_token');
|
||||||
|
if (sessionToken) {
|
||||||
|
const { data, error: fnError } = await supabase.functions.invoke('telegram-auth', {
|
||||||
|
body: { sessionToken }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (fnError) throw fnError;
|
||||||
|
if (!data?.user) throw new Error('Authentication failed');
|
||||||
|
|
||||||
|
setUser(data.user);
|
||||||
|
const p2pUserId = data.auth_user_id || data.user.id;
|
||||||
|
setCurrentUserId(p2pUserId);
|
||||||
|
|
||||||
|
if (data.session_token) {
|
||||||
|
localStorage.setItem('p2p_session', data.session_token);
|
||||||
|
}
|
||||||
|
if (data.auth_user_id) {
|
||||||
|
localStorage.setItem('p2p_auth_user_id', data.auth_user_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.history.replaceState({}, '', window.location.pathname);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No auth method available
|
||||||
|
throw new Error('Please open from Telegram MiniApp');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const message = err instanceof Error ? err.message : 'Login failed';
|
const message = err instanceof Error ? err.message : 'Login failed';
|
||||||
setError(message);
|
setError(message);
|
||||||
window.Telegram?.WebApp.HapticFeedback.notificationOccurred('error');
|
window.Telegram?.WebApp?.HapticFeedback?.notificationOccurred('error');
|
||||||
console.error('Login error:', err);
|
console.error('Login error:', err);
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
|||||||
Reference in new issue
Block a user