freealberta/influence/app/public/js/verify-email.js

90 lines
3.1 KiB
JavaScript

// Email Verification Handler
document.addEventListener('DOMContentLoaded', async () => {
const statusDiv = document.getElementById('verification-status');
// Get token from URL query parameters
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get('token');
if (!token) {
showError('Invalid verification link - no token provided');
return;
}
try {
// Call API to verify token
const response = await fetch(`/api/verify-email/${token}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include' // Important for session management
});
const data = await response.json();
if (data.success) {
// Store campaign data in session storage for dashboard
if (data.campaignData) {
sessionStorage.setItem('pendingCampaign', JSON.stringify(data.campaignData));
}
// Show success and redirect
if (data.isNewUser) {
showSuccess(
'✅ Welcome! Your account has been created successfully. Check your email for login credentials. Your email content is ready - just add a campaign title and description!',
'/dashboard.html',
'Go to Dashboard',
4000
);
} else if (data.needsAccount) {
showSuccess(
'Email verified! Please create your account to continue.',
'/login.html',
'Go to Login',
3000
);
} else {
showSuccess(
'Email verified! Redirecting to campaign creation...',
'/dashboard.html',
'Go to Dashboard',
2000
);
}
} else {
showError(data.error || 'Verification failed');
}
} catch (error) {
console.error('Verification error:', error);
showError('An error occurred during verification. Please try again or contact support.');
}
});
function showSuccess(message, redirectUrl, buttonText, autoRedirectDelay = 3000) {
const statusDiv = document.getElementById('verification-status');
statusDiv.innerHTML = `
<div class="success-icon">✓</div>
<h2 style="color: #28a745;">Verification Successful!</h2>
<p class="message">${message}</p>
<a href="${redirectUrl}" class="btn">${buttonText}</a>
`;
// Auto-redirect after delay
setTimeout(() => {
window.location.href = redirectUrl;
}, autoRedirectDelay);
}
function showError(errorMessage) {
const statusDiv = document.getElementById('verification-status');
statusDiv.innerHTML = `
<div class="error-icon">✗</div>
<h2 style="color: #dc3545;">Verification Failed</h2>
<p class="message">${errorMessage}</p>
<a href="/" class="btn" style="background-color: #666;">Return to Home</a>
`;
}