Skip to content

Commit 8f43685

Browse files
committed
Add debug logging and improve error handling for Supabase auth flow
1 parent 801748f commit 8f43685

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

public/js/main.js

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,14 @@ function initLoginPage() {
188188
const { createClient } = await import('https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2/+esm');
189189
const supabaseUrl = 'https://arokhsfbkdnfuklmqajh.supabase.co'; // Should match server config
190190
const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFyb2toc2Zia2RuZnVrbG1xYWpoIiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODI0NTI4MDAsImV4cCI6MTk5ODAyODgwMH0.KxwHdxWXLLrJtFzLAYI-fwzgz8m5xsHD4XGdNw_xJm8'; // Public anon key
191+
192+
console.log('Creating Supabase client with URL:', supabaseUrl);
193+
console.log('Anon key exists:', !!supabaseAnonKey);
194+
191195
const supabase = createClient(supabaseUrl, supabaseAnonKey);
196+
console.log('Supabase client created successfully');
197+
198+
console.log('Attempting to sign in with Supabase:', email);
192199

193200
// Sign in with Supabase to get JWT token
194201
const { data: authData, error: authError } = await supabase.auth.signInWithPassword({
@@ -207,15 +214,34 @@ function initLoginPage() {
207214

208215
// Store JWT token in localStorage
209216
localStorage.setItem('jwt_token', authData.session.access_token);
210-
console.log('JWT token stored successfully');
217+
console.log('JWT token stored successfully, length:', authData.session.access_token.length);
218+
console.log('JWT token preview:', authData.session.access_token.substring(0, 10) + '...');
219+
220+
try {
221+
// Check subscription status using the JWT token
222+
console.log('Checking subscription status for:', email);
223+
const subscriptionStatus = await ApiClient.checkSubscriptionStatus(email);
224+
console.log('Subscription status:', subscriptionStatus);
225+
} catch (subscriptionError) {
226+
console.error('Error checking subscription status:', subscriptionError);
227+
// Continue even if subscription check fails
228+
// We'll handle this case below
229+
}
211230

212-
// Check subscription status using the JWT token
213-
const subscriptionStatus = await ApiClient.checkSubscriptionStatus(email);
214-
console.log('Subscription status:', subscriptionStatus);
231+
// Get subscription status from the try/catch block above
232+
let subscriptionStatus = null;
233+
try {
234+
subscriptionStatus = await ApiClient.checkSubscriptionStatus(email);
235+
} catch (error) {
236+
console.warn('Could not verify subscription status, proceeding with login anyway');
237+
}
215238

216-
if (subscriptionStatus.has_subscription) {
239+
// Store username regardless of subscription status
240+
localStorage.setItem('username', email);
241+
242+
if (subscriptionStatus && subscriptionStatus.has_subscription) {
243+
console.log('User has an active subscription');
217244
// User has an active subscription
218-
localStorage.setItem('username', email);
219245
localStorage.setItem('subscription_data', JSON.stringify(subscriptionStatus));
220246

221247
// Create and store a publicly accessible user object
@@ -230,33 +256,45 @@ function initLoginPage() {
230256
createdAt: new Date().toISOString()
231257
};
232258
localStorage.setItem('user', JSON.stringify(userObject));
233-
234-
// Dispatch auth changed event
235-
window.dispatchEvent(new CustomEvent('auth-changed'));
236-
237-
// Redirect to the API keys page
238-
window.router.navigate('/api-keys');
239259
} else {
240-
// User doesn't have an active subscription
241-
submitButton.textContent = originalButtonText;
242-
submitButton.disabled = false;
243-
244-
// Show error message with option to register
245-
const register = confirm(
246-
'No active subscription found for this email. Would you like to register and subscribe?'
247-
);
248-
249-
if (register) {
250-
window.router.navigate('/register');
251-
}
260+
console.log('No subscription data available or user has no active subscription');
261+
// Create a basic user object without subscription data
262+
const userObject = {
263+
email: email,
264+
username: email,
265+
subscription: {
266+
status: 'unknown'
267+
},
268+
createdAt: new Date().toISOString()
269+
};
270+
localStorage.setItem('user', JSON.stringify(userObject));
252271
}
272+
273+
// Dispatch auth changed event
274+
window.dispatchEvent(new CustomEvent('auth-changed'));
275+
276+
// Redirect to the API keys page
277+
console.log('Login successful, redirecting to API keys page');
278+
window.router.navigate('/api-keys');
253279
} catch (error) {
254280
console.error('Login error:', error);
281+
console.error('Error stack:', error.stack);
255282
submitButton.textContent = originalButtonText;
256283
submitButton.disabled = false;
257284

258-
// Show error message
259-
alert('Login failed: ' + (error.message || 'Unable to check subscription status'));
285+
// Provide more specific error messages based on the error type
286+
let errorMessage = 'Login failed: ';
287+
288+
if (error.message && error.message.includes('API key')) {
289+
errorMessage += 'Invalid credentials. Please check your email and password.';
290+
} else if (error.message && error.message.includes('session')) {
291+
errorMessage += 'Authentication server error. Please try again later.';
292+
} else {
293+
errorMessage += (error.message || 'Unable to complete login process');
294+
}
295+
296+
console.error('Showing error message to user:', errorMessage);
297+
alert(errorMessage);
260298
}
261299
});
262300
}
@@ -330,7 +368,12 @@ function initRegisterPage() {
330368
const { createClient } = await import('https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2/+esm');
331369
const supabaseUrl = 'https://arokhsfbkdnfuklmqajh.supabase.co'; // Should match server config
332370
const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFyb2toc2Zia2RuZnVrbG1xYWpoIiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODI0NTI4MDAsImV4cCI6MTk5ODAyODgwMH0.KxwHdxWXLLrJtFzLAYI-fwzgz8m5xsHD4XGdNw_xJm8'; // Public anon key
371+
372+
console.log('Creating Supabase client for registration with URL:', supabaseUrl);
373+
console.log('Anon key exists:', !!supabaseAnonKey);
374+
333375
const supabase = createClient(supabaseUrl, supabaseAnonKey);
376+
console.log('Supabase client created successfully for registration');
334377

335378
// Register user with Supabase to get JWT token
336379
const { data: authData, error: authError } = await supabase.auth.signUp({

0 commit comments

Comments
 (0)