File size: 3,929 Bytes
58c1398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * 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.');
        }
    });
}