mirror of
https://github.com/pezkuwichain/pezkuwi-telegram-miniapp.git
synced 2026-04-22 01:57:56 +00:00
debug: add detailed logging to auth flow
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pezkuwi-telegram-miniapp",
|
||||
"version": "1.0.149",
|
||||
"version": "1.0.150",
|
||||
"type": "module",
|
||||
"description": "Pezkuwichain Telegram Mini App - Forum, Announcements, Rewards",
|
||||
"author": "Pezkuwichain Team",
|
||||
|
||||
@@ -59,16 +59,24 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const authAttempted = useRef(false);
|
||||
|
||||
const signIn = useCallback(async () => {
|
||||
console.warn('[Auth] signIn called');
|
||||
console.warn('[Auth] ========== signIn START ==========');
|
||||
const tg = window.Telegram?.WebApp;
|
||||
console.warn('[Auth] Telegram WebApp:', tg ? 'exists' : 'missing');
|
||||
console.warn('[Auth] platform:', tg?.platform, '| version:', tg?.version);
|
||||
console.warn('[Auth] TG object:', tg ? 'exists' : 'MISSING');
|
||||
console.warn(
|
||||
'[Auth] TG.initData direct check:',
|
||||
tg?.initData ? tg.initData.length + ' chars' : 'EMPTY'
|
||||
);
|
||||
|
||||
setAuthError(null);
|
||||
setIsLoading(true);
|
||||
|
||||
// Wait for initData to be available (retry mechanism)
|
||||
console.warn('[Auth] Calling waitForInitData...');
|
||||
const initData = await waitForInitData();
|
||||
console.warn(
|
||||
'[Auth] waitForInitData returned:',
|
||||
initData ? initData.length + ' chars' : 'NULL'
|
||||
);
|
||||
|
||||
if (!initData) {
|
||||
console.warn('[Auth] No initData after waiting, setting error');
|
||||
@@ -77,12 +85,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.warn('[Auth] initData available:', initData.length, 'chars');
|
||||
console.warn('[Auth] initData available, calling signInWithTelegram...');
|
||||
|
||||
try {
|
||||
console.warn('[Auth] Calling signInWithTelegram...');
|
||||
const result = await signInWithTelegram(initData);
|
||||
console.warn('[Auth] signInWithTelegram result:', JSON.stringify(result));
|
||||
console.warn('[Auth] signInWithTelegram SUCCESS:', JSON.stringify(result));
|
||||
if (result?.user) {
|
||||
setUser(result.user);
|
||||
setAuthError(null);
|
||||
@@ -91,22 +98,17 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
console.warn('[Auth] No user in result');
|
||||
setAuthError('No user returned from auth');
|
||||
}
|
||||
// Store session token for P2P and other cross-app auth
|
||||
if (result?.session_token) {
|
||||
setSessionToken(result.session_token);
|
||||
console.warn('[Auth] Session token set');
|
||||
} else {
|
||||
console.warn('[Auth] No session_token in result');
|
||||
}
|
||||
} catch (error) {
|
||||
// Capture error message for debugging
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
console.warn('[Auth] signInWithTelegram FAILED:', errorMsg);
|
||||
setAuthError(errorMsg);
|
||||
if (import.meta.env.DEV) {
|
||||
console.error('[Auth] Error:', error);
|
||||
}
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
console.warn('[Auth] ========== signIn END ==========');
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
||||
+43
-29
@@ -7,48 +7,62 @@ export const supabase: SupabaseClient = createClient(env.SUPABASE_URL, env.SUPAB
|
||||
|
||||
// Telegram auth helper - validates initData with Edge Function
|
||||
export async function signInWithTelegram(initData: string) {
|
||||
console.warn('[signInWithTelegram] Called with initData:', initData.length, 'chars');
|
||||
|
||||
if (!initData) {
|
||||
console.warn('[signInWithTelegram] No initData provided!');
|
||||
throw new Error('No Telegram initData provided');
|
||||
}
|
||||
|
||||
const { data, error } = await supabase.functions.invoke('telegram-auth', {
|
||||
body: { initData },
|
||||
});
|
||||
console.warn('[signInWithTelegram] Calling supabase.functions.invoke...');
|
||||
|
||||
if (error) {
|
||||
// Extract more detailed error message
|
||||
let errorMessage = error.message || 'Unknown error';
|
||||
try {
|
||||
const { data, error } = await supabase.functions.invoke('telegram-auth', {
|
||||
body: { initData },
|
||||
});
|
||||
|
||||
// Check if there's additional context in the error
|
||||
if (error.context?.body) {
|
||||
try {
|
||||
const bodyError = JSON.parse(error.context.body);
|
||||
if (bodyError.error) {
|
||||
errorMessage = bodyError.error;
|
||||
}
|
||||
} catch {
|
||||
// Body is not JSON, use as-is
|
||||
if (typeof error.context.body === 'string') {
|
||||
errorMessage = error.context.body;
|
||||
console.warn('[signInWithTelegram] Response - data:', !!data, 'error:', !!error);
|
||||
|
||||
if (error) {
|
||||
let errorMessage = error.message || 'Unknown error';
|
||||
console.warn('[signInWithTelegram] Error object:', JSON.stringify(error));
|
||||
|
||||
if (error.context?.body) {
|
||||
try {
|
||||
const bodyError = JSON.parse(error.context.body);
|
||||
if (bodyError.error) {
|
||||
errorMessage = bodyError.error;
|
||||
}
|
||||
} catch {
|
||||
if (typeof error.context.body === 'string') {
|
||||
errorMessage = error.context.body;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.warn('[signInWithTelegram] Final error message:', errorMessage);
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
console.error('[Auth] Telegram sign-in failed:', errorMessage);
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
if (data?.error) {
|
||||
console.warn('[signInWithTelegram] Data contains error:', data.error);
|
||||
throw new Error(data.error);
|
||||
}
|
||||
|
||||
// Check if edge function returned an error in data
|
||||
if (data?.error) {
|
||||
console.error('[Auth] Edge function error:', data.error);
|
||||
throw new Error(data.error);
|
||||
}
|
||||
console.warn('[signInWithTelegram] Success, user:', data?.user?.first_name);
|
||||
|
||||
if (data?.session) {
|
||||
await supabase.auth.setSession(data.session);
|
||||
}
|
||||
if (data?.session) {
|
||||
await supabase.auth.setSession(data.session);
|
||||
}
|
||||
|
||||
return data;
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
'[signInWithTelegram] Exception caught:',
|
||||
e instanceof Error ? e.message : String(e)
|
||||
);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to get current session
|
||||
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.0.149",
|
||||
"buildTime": "2026-02-07T02:40:20.478Z",
|
||||
"buildNumber": 1770432020479
|
||||
"version": "1.0.150",
|
||||
"buildTime": "2026-02-07T02:53:36.684Z",
|
||||
"buildNumber": 1770432816685
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user