Refactor email verification to use JSON responses instead of redirects
All checks were successful
Build And Push Image / docker (push) Successful in 2m54s
All checks were successful
Build And Push Image / docker (push) Successful in 2m54s
- Replace server-side redirects with JSON API responses for better error handling - Add support for partial success when Keycloak update fails but token is valid - Improve error messages with specific status codes (410 for expired, 409 for already used) - Extract email from API response instead of URL query parameters - Enable client-side navigation with proper error state management
This commit is contained in:
@@ -173,27 +173,45 @@ const verifyEmail = async () => {
|
||||
// Call the API endpoint to verify the email
|
||||
const response = await $fetch(`/api/auth/verify-email?token=${token.value}`, {
|
||||
method: 'GET'
|
||||
});
|
||||
}) as any;
|
||||
|
||||
console.log('[auth/verify] Email verification successful:', response);
|
||||
|
||||
// Extract email from response
|
||||
const email = response?.data?.email || '';
|
||||
const partialSuccess = response?.data?.partialSuccess || false;
|
||||
|
||||
// Redirect to success page with email info
|
||||
const email = route.query.email || '';
|
||||
const redirectUrl = `/auth/verify-success${email ? `?email=${encodeURIComponent(email as string)}` : ''}`;
|
||||
let redirectUrl = `/auth/verify-success`;
|
||||
const queryParams = [];
|
||||
|
||||
if (email) {
|
||||
queryParams.push(`email=${encodeURIComponent(email)}`);
|
||||
}
|
||||
|
||||
if (partialSuccess) {
|
||||
queryParams.push('warning=partial');
|
||||
}
|
||||
|
||||
if (queryParams.length > 0) {
|
||||
redirectUrl += '?' + queryParams.join('&');
|
||||
}
|
||||
|
||||
await navigateTo(redirectUrl);
|
||||
|
||||
} catch (err: any) {
|
||||
console.error('[auth/verify] Email verification failed:', err);
|
||||
|
||||
if (err.statusCode === 400) {
|
||||
error.value = 'Invalid or expired verification token. Please request a new verification email.';
|
||||
if (err.statusCode === 410) {
|
||||
error.value = 'Verification link has expired. Please request a new verification email.';
|
||||
} else if (err.statusCode === 409) {
|
||||
error.value = 'This verification link has already been used or is invalid.';
|
||||
} else if (err.statusCode === 400) {
|
||||
error.value = 'Invalid verification token. Please request a new verification email.';
|
||||
} else if (err.statusCode === 404) {
|
||||
error.value = 'User not found. The verification token may be invalid.';
|
||||
} else if (err.statusCode === 409) {
|
||||
error.value = 'Email is already verified. You can now log in to your account.';
|
||||
} else {
|
||||
error.value = err.message || 'Email verification failed. Please try again or contact support.';
|
||||
error.value = err.data?.message || err.message || 'Email verification failed. Please try again or contact support.';
|
||||
}
|
||||
|
||||
verifying.value = false;
|
||||
|
||||
Reference in New Issue
Block a user