mirror of
https://github.com/pezkuwichain/pezkuwi-telegram-miniapp.git
synced 2026-06-20 11:31:05 +00:00
fix: show actual error message on reaction failure
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pezkuwi-telegram-miniapp",
|
||||
"version": "1.0.152",
|
||||
"version": "1.0.153",
|
||||
"type": "module",
|
||||
"description": "Pezkuwichain Telegram Mini App - Forum, Announcements, Rewards",
|
||||
"author": "Pezkuwichain Team",
|
||||
|
||||
@@ -22,24 +22,20 @@ interface AuthContextType {
|
||||
const AuthContext = createContext<AuthContextType | null>(null);
|
||||
|
||||
// Wait for Telegram SDK to be ready with initData
|
||||
// 25 attempts * 200ms = 5 seconds max wait
|
||||
function waitForInitData(maxAttempts = 25, intervalMs = 200): Promise<string | null> {
|
||||
return new Promise((resolve) => {
|
||||
let attempts = 0;
|
||||
|
||||
const check = () => {
|
||||
attempts++;
|
||||
const tg = window.Telegram?.WebApp;
|
||||
const initData = tg?.initData;
|
||||
const initData = window.Telegram?.WebApp?.initData;
|
||||
|
||||
if (initData && initData.length > 0) {
|
||||
console.warn(`[Auth] initData found after ${attempts} attempts`);
|
||||
resolve(initData);
|
||||
return;
|
||||
}
|
||||
|
||||
if (attempts >= maxAttempts) {
|
||||
console.warn(`[Auth] initData not found after ${attempts} attempts`);
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
@@ -59,56 +55,33 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const authAttempted = useRef(false);
|
||||
|
||||
const signIn = useCallback(async () => {
|
||||
console.warn('[Auth] ========== signIn START ==========');
|
||||
const tg = window.Telegram?.WebApp;
|
||||
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');
|
||||
setAuthError('No Telegram initData');
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
console.warn('[Auth] initData available, calling signInWithTelegram...');
|
||||
|
||||
try {
|
||||
const result = await signInWithTelegram(initData);
|
||||
console.warn('[Auth] signInWithTelegram SUCCESS:', JSON.stringify(result));
|
||||
if (result?.user) {
|
||||
setUser(result.user);
|
||||
setAuthError(null);
|
||||
console.warn('[Auth] User set:', result.user.first_name);
|
||||
} else {
|
||||
console.warn('[Auth] No user in result');
|
||||
setAuthError('No user returned from auth');
|
||||
}
|
||||
if (result?.session_token) {
|
||||
setSessionToken(result.session_token);
|
||||
console.warn('[Auth] Session token set');
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||
console.warn('[Auth] signInWithTelegram FAILED:', errorMsg);
|
||||
setAuthError(errorMsg);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
console.warn('[Auth] ========== signIn END ==========');
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
||||
+25
-47
@@ -7,62 +7,40 @@ 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');
|
||||
}
|
||||
|
||||
console.warn('[signInWithTelegram] Calling supabase.functions.invoke...');
|
||||
const { data, error } = await supabase.functions.invoke('telegram-auth', {
|
||||
body: { initData },
|
||||
});
|
||||
|
||||
try {
|
||||
const { data, error } = await supabase.functions.invoke('telegram-auth', {
|
||||
body: { initData },
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
if (error) {
|
||||
let errorMessage = error.message || 'Unknown 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);
|
||||
}
|
||||
|
||||
if (data?.error) {
|
||||
console.warn('[signInWithTelegram] Data contains 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);
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
'[signInWithTelegram] Exception caught:',
|
||||
e instanceof Error ? e.message : String(e)
|
||||
);
|
||||
throw e;
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
if (data?.error) {
|
||||
throw new Error(data.error);
|
||||
}
|
||||
|
||||
if (data?.session) {
|
||||
await supabase.auth.setSession(data.session);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// Helper to get current session
|
||||
|
||||
@@ -29,7 +29,10 @@ export function AnnouncementsSection() {
|
||||
{ announcementId: id, reaction },
|
||||
{
|
||||
onSuccess: () => hapticNotification('success'),
|
||||
onError: () => hapticNotification('error'),
|
||||
onError: (err) => {
|
||||
hapticNotification('error');
|
||||
window.Telegram?.WebApp?.showAlert(err.message || 'Çewtî');
|
||||
},
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.0.152",
|
||||
"buildTime": "2026-02-07T03:21:34.655Z",
|
||||
"buildNumber": 1770434494656
|
||||
"version": "1.0.153",
|
||||
"buildTime": "2026-02-07T03:32:49.644Z",
|
||||
"buildNumber": 1770435169644
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user