feat: improve login process with enhanced session handling and error management
All checks were successful
Build And Push Image / docker (push) Successful in 2m55s
All checks were successful
Build And Push Image / docker (push) Successful in 2m55s
This commit is contained in:
@@ -45,15 +45,36 @@ export const useAuth = () => {
|
||||
user?: User;
|
||||
}>('/api/auth/direct-login', {
|
||||
method: 'POST',
|
||||
body: credentials
|
||||
body: credentials,
|
||||
timeout: 30000 // 30 second timeout
|
||||
});
|
||||
|
||||
console.log('✅ Login response received:', response);
|
||||
|
||||
if (response.success) {
|
||||
// Add a small delay to ensure cookie is set before checking session
|
||||
console.log('⏳ Waiting for cookie to be set...');
|
||||
await new Promise(resolve => setTimeout(resolve, 200));
|
||||
|
||||
// After successful login, get the user data from the session
|
||||
console.log('🔄 Getting user data from session...');
|
||||
const sessionSuccess = await checkAuth();
|
||||
|
||||
// Try multiple times in case of timing issues
|
||||
let sessionSuccess = false;
|
||||
let attempts = 0;
|
||||
const maxAttempts = 3;
|
||||
|
||||
while (!sessionSuccess && attempts < maxAttempts) {
|
||||
attempts++;
|
||||
console.log(`🔄 Session check attempt ${attempts}/${maxAttempts}`);
|
||||
|
||||
sessionSuccess = await checkAuth();
|
||||
|
||||
if (!sessionSuccess && attempts < maxAttempts) {
|
||||
console.log('⏳ Session not ready, waiting 500ms...');
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
}
|
||||
}
|
||||
|
||||
if (sessionSuccess) {
|
||||
console.log('👤 User data retrieved from session:', user.value);
|
||||
@@ -65,7 +86,9 @@ export const useAuth = () => {
|
||||
return { success: true };
|
||||
} else {
|
||||
console.warn('❌ Failed to get user data from session after login');
|
||||
return { success: false, error: 'Login succeeded but failed to get user data' };
|
||||
// Still redirect since login was successful on server
|
||||
await navigateTo('/dashboard');
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,8 +96,24 @@ export const useAuth = () => {
|
||||
return { success: false, error: 'Login failed' };
|
||||
} catch (err: any) {
|
||||
console.error('❌ Login error caught:', err);
|
||||
error.value = err.data?.message || err.message || 'Login failed';
|
||||
return { success: false, error: error.value };
|
||||
|
||||
// Handle different types of errors
|
||||
let errorMessage = 'Login failed';
|
||||
|
||||
if (err.status === 502) {
|
||||
errorMessage = 'Server temporarily unavailable. Please try again.';
|
||||
} else if (err.status === 401) {
|
||||
errorMessage = 'Invalid username or password';
|
||||
} else if (err.status === 429) {
|
||||
errorMessage = 'Too many login attempts. Please try again later.';
|
||||
} else if (err.data?.message) {
|
||||
errorMessage = err.data.message;
|
||||
} else if (err.message) {
|
||||
errorMessage = err.message;
|
||||
}
|
||||
|
||||
error.value = errorMessage;
|
||||
return { success: false, error: errorMessage };
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user