Dilip8756's picture
Upload 100 files
58c1398 verified
/**
* Authentication Logic - Aadhaar Pro
*/
document.addEventListener('DOMContentLoaded', () => {
setupLoginForm();
setupSignupForm();
setupAuthThemeToggle();
});
function setupAuthThemeToggle() {
const themeToggleBtn = document.getElementById('theme-toggle-btn');
if (themeToggleBtn) {
themeToggleBtn.addEventListener('click', () => {
const root = document.documentElement;
const currentTheme = root.getAttribute('data-theme') || 'light';
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
if (newTheme === 'dark') {
root.setAttribute('data-theme', 'dark');
} else {
root.removeAttribute('data-theme');
}
localStorage.setItem('theme', newTheme);
});
}
}
function showAuthError(elementId, message) {
const errorDiv = document.getElementById(elementId);
if (errorDiv) {
errorDiv.innerHTML = `<i class="fa-solid fa-triangle-exclamation"></i> ${message}`;
errorDiv.style.display = 'flex';
// Horizontal shake animation only on the error message
errorDiv.style.animation = 'none';
errorDiv.offsetHeight; /* trigger reflow */
errorDiv.style.animation = 'errorShake 0.4s ease-in-out';
}
}
function setupLoginForm() {
const loginForm = document.getElementById('login-form');
if (!loginForm) return;
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const phone = document.getElementById('phone').value.trim();
const password = document.getElementById('password').value.trim();
if (!phone || !password) return;
try {
const res = await fetch('/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ phone, password })
});
const data = await res.json();
if (data.success) {
window.location.href = '/';
} else {
showAuthError('login-error', 'User not found or incorrect credentials. <a href="/signup">Create Account</a>');
}
} catch (err) {
console.error('Login error:', err);
showAuthError('login-error', 'A connection error occurred. Please try again later.');
}
});
}
function setupSignupForm() {
const signupForm = document.getElementById('signup-form');
if (!signupForm) return;
signupForm.addEventListener('submit', async (e) => {
e.preventDefault();
const data = {
name: document.getElementById('name').value.trim(),
phone: document.getElementById('phone').value.trim(),
password: document.getElementById('password').value.trim()
};
if (!data.name || !data.phone || !data.password) return;
try {
const res = await fetch('/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const resData = await res.json();
if (resData.success) {
// If successful signup, we can redirect to login with a special hash or query param,
// but standard alert is okay before redirect so they know it worked. Or just redirect.
alert('Account created successfully! Please login.');
window.location.href = '/login';
} else {
showAuthError('signup-error', resData.error || 'Registration failed. Phone might be in use.');
}
} catch (err) {
console.error('Signup error:', err);
showAuthError('signup-error', 'An error occurred during registration. Please try again.');
}
});
}