Refactor login error handling and improve user feedback
- Update LoginForm component to display error messages directly from the authentication response. - Modify useAuth composable to handle login errors more effectively, ensuring proper error messages are thrown. - Enhance login API response to return structured error messages for invalid credentials. - Adjust Cidaas utility to throw specific errors for invalid username/password scenarios.
This commit is contained in:
@@ -17,26 +17,32 @@ export function useAuth() {
|
||||
* Direct authentication via Cidaas API (no redirect)
|
||||
*/
|
||||
async function login(email: string, password: string) {
|
||||
try {
|
||||
// Call login endpoint - creates session directly
|
||||
await $fetch('/api/auth/login', {
|
||||
method: 'POST',
|
||||
body: { email, password },
|
||||
})
|
||||
// Call login endpoint - creates session directly
|
||||
const response = await $fetch<{ success: boolean; message?: string }>('/api/auth/login', {
|
||||
method: 'POST',
|
||||
body: { email, password },
|
||||
// Don't throw on 4xx/5xx, we handle the response ourselves
|
||||
ignoreResponseError: true,
|
||||
})
|
||||
|
||||
// Refresh user session
|
||||
await fetch()
|
||||
|
||||
// Redirect to homepage or saved destination
|
||||
const redirectAfterLogin = useCookie('redirect_after_login')
|
||||
const destination = redirectAfterLogin.value || '/'
|
||||
redirectAfterLogin.value = null // Clear cookie
|
||||
|
||||
navigateTo(destination)
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error)
|
||||
// Check if login was successful
|
||||
if (!response.success) {
|
||||
// Throw error with the server's message (contains German text with umlauts)
|
||||
const error: any = new Error(response.message || 'Login failed')
|
||||
error.data = response
|
||||
error.statusCode = 401
|
||||
throw error
|
||||
}
|
||||
|
||||
// Refresh user session
|
||||
await fetch()
|
||||
|
||||
// Redirect to homepage or saved destination
|
||||
const redirectAfterLogin = useCookie('redirect_after_login')
|
||||
const destination = redirectAfterLogin.value || '/'
|
||||
redirectAfterLogin.value = null // Clear cookie
|
||||
|
||||
navigateTo(destination)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user