2025-08-08 22:51:14 +02:00
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
|
try {
|
|
|
|
|
const { token } = getQuery(event);
|
|
|
|
|
|
|
|
|
|
if (!token || typeof token !== 'string') {
|
2025-08-13 17:16:22 +02:00
|
|
|
console.log('[verify-email] Missing or invalid token, redirecting to expired page');
|
|
|
|
|
return sendRedirect(event, '/auth/verify-expired?reason=invalid', 302);
|
2025-08-08 22:51:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('[verify-email] Processing verification token...');
|
|
|
|
|
|
2025-08-10 15:48:11 +02:00
|
|
|
// Verify the token WITHOUT consuming it yet
|
|
|
|
|
const { verifyEmailToken, consumeEmailToken } = await import('~/server/utils/email-tokens');
|
2025-08-08 22:51:14 +02:00
|
|
|
const { userId, email } = await verifyEmailToken(token);
|
|
|
|
|
|
|
|
|
|
// Update user verification status in Keycloak
|
|
|
|
|
const { createKeycloakAdminClient } = await import('~/server/utils/keycloak-admin');
|
|
|
|
|
const keycloak = createKeycloakAdminClient();
|
2025-08-09 19:40:04 +02:00
|
|
|
|
|
|
|
|
let partialSuccess = false;
|
2025-08-10 15:48:11 +02:00
|
|
|
let keycloakError = null;
|
2025-08-08 22:51:14 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await keycloak.updateUserProfile(userId, {
|
|
|
|
|
emailVerified: true,
|
|
|
|
|
attributes: {
|
|
|
|
|
lastLoginDate: new Date().toISOString()
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log('[verify-email] Successfully verified user:', userId, 'email:', email);
|
|
|
|
|
|
2025-08-10 15:48:11 +02:00
|
|
|
// ONLY consume token after successful Keycloak update
|
|
|
|
|
await consumeEmailToken(token);
|
|
|
|
|
|
2025-08-13 17:16:22 +02:00
|
|
|
} catch (keycloakUpdateError: any) {
|
|
|
|
|
console.error('[verify-email] Keycloak update failed:', keycloakUpdateError.message);
|
2025-08-10 15:48:11 +02:00
|
|
|
|
|
|
|
|
// Check if this is a retryable error or a permanent failure
|
2025-08-13 17:16:22 +02:00
|
|
|
if (keycloakUpdateError.message?.includes('error-user-attribute-required')) {
|
2025-08-10 15:48:11 +02:00
|
|
|
// This is a configuration issue - don't consume token, allow retries
|
|
|
|
|
console.log('[verify-email] Keycloak configuration error - token preserved for retry');
|
|
|
|
|
partialSuccess = true;
|
2025-08-13 17:16:22 +02:00
|
|
|
keycloakError = keycloakUpdateError.message;
|
2025-08-10 15:48:11 +02:00
|
|
|
} else {
|
|
|
|
|
// For other errors, still consume token to prevent infinite retries
|
|
|
|
|
console.log('[verify-email] Consuming token despite Keycloak error to prevent loops');
|
|
|
|
|
await consumeEmailToken(token);
|
|
|
|
|
partialSuccess = true;
|
2025-08-13 17:16:22 +02:00
|
|
|
keycloakError = keycloakUpdateError.message;
|
2025-08-10 15:48:11 +02:00
|
|
|
}
|
2025-08-08 22:51:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-13 17:16:22 +02:00
|
|
|
// Build success redirect URL with query parameters
|
|
|
|
|
const successUrl = new URL('/auth/verify-success', 'https://portal.monacousa.org');
|
|
|
|
|
successUrl.searchParams.set('email', email);
|
|
|
|
|
|
|
|
|
|
if (partialSuccess && keycloakError) {
|
|
|
|
|
successUrl.searchParams.set('warning', 'partial');
|
|
|
|
|
console.log('[verify-email] Redirecting to success page with partial warning');
|
|
|
|
|
} else {
|
|
|
|
|
console.log('[verify-email] Redirecting to success page - verification complete');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Redirect to success page instead of returning JSON
|
|
|
|
|
return sendRedirect(event, successUrl.pathname + successUrl.search, 302);
|
2025-08-09 19:40:04 +02:00
|
|
|
|
2025-08-08 22:51:14 +02:00
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('[verify-email] Verification failed:', error.message);
|
|
|
|
|
|
2025-08-13 17:16:22 +02:00
|
|
|
// Redirect to appropriate error page instead of throwing errors
|
2025-08-08 22:51:14 +02:00
|
|
|
if (error.message?.includes('expired')) {
|
2025-08-13 17:16:22 +02:00
|
|
|
console.log('[verify-email] Token expired, redirecting to expired page');
|
|
|
|
|
return sendRedirect(event, '/auth/verify-expired?reason=expired', 302);
|
|
|
|
|
} else if (error.message?.includes('already used')) {
|
|
|
|
|
console.log('[verify-email] Token already used, redirecting to expired page');
|
|
|
|
|
return sendRedirect(event, '/auth/verify-expired?reason=used', 302);
|
|
|
|
|
} else if (error.message?.includes('not found')) {
|
|
|
|
|
console.log('[verify-email] Token not found, redirecting to expired page');
|
|
|
|
|
return sendRedirect(event, '/auth/verify-expired?reason=invalid', 302);
|
2025-08-08 22:51:14 +02:00
|
|
|
} else {
|
2025-08-13 17:16:22 +02:00
|
|
|
console.log('[verify-email] Generic verification error, redirecting to expired page');
|
|
|
|
|
return sendRedirect(event, '/auth/verify-expired?reason=invalid', 302);
|
2025-08-08 22:51:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|