File size: 7,990 Bytes
390cffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Authentication UI management
let pendingAction = null;

// Initialize auth UI when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
    initAuthUI();
    updateNavbar();
});

// Initialize authentication UI
function initAuthUI() {
    // Modal functionality
    const modal = document.getElementById('loginModal');
    const closeBtn = document.getElementById('modalClose');
    const demoBtn = document.getElementById('demoLoginBtn');

    if (closeBtn) {
        closeBtn.addEventListener('click', closeModal);
    }

    if (demoBtn) {
        demoBtn.addEventListener('click', handleDemoLogin);
    }

    // Close modal on backdrop click
    if (modal) {
        modal.addEventListener('click', (e) => {
            if (e.target === modal) {
                closeModal();
            }
        });
    }

    // Close modal on Escape key
    document.addEventListener('keydown', (e) => {
        if (e.key === 'Escape' && isModalOpen()) {
            closeModal();
        }
    });
}

// Show login modal
function showLoginModal(action = null) {
    pendingAction = action;
    const modal = document.getElementById('loginModal');
    if (modal) {
        modal.style.display = 'flex';
        document.body.style.overflow = 'hidden';
        
        // Focus management
        const focusableElements = modal.querySelectorAll('button, a');
        if (focusableElements.length > 0) {
            focusableElements[0].focus();
        }
    }
}

// Close login modal
function closeModal() {
    const modal = document.getElementById('loginModal');
    if (modal) {
        modal.style.display = 'none';
        document.body.style.overflow = '';
        pendingAction = null;
    }
}

// Check if modal is open
function isModalOpen() {
    const modal = document.getElementById('loginModal');
    return modal && modal.style.display === 'flex';
}

// Handle demo login from modal
async function handleDemoLogin() {
    try {
        const result = await auth.loginAsDemo();
        
        if (result.success) {
            auth.showToast(`Welcome, ${result.user.name}!`, 'success');
            closeModal();
            updateNavbar();
            
            // Execute pending action if any
            if (pendingAction && typeof pendingAction === 'function') {
                pendingAction();
            }
        } else {
            auth.showToast(result.error || 'Demo login failed', 'error');
        }
    } catch (error) {
        auth.showToast('An error occurred. Please try again.', 'error');
    }
}

// Update navbar based on auth state
function updateNavbar() {
    const navActions = document.querySelector('.nav-actions');
    if (!navActions) return;

    if (auth.isAuthenticated()) {
        const user = auth.getUser();
        
        // Create user dropdown
        navActions.innerHTML = `
            <div class="user-dropdown">
                <button class="user-avatar" id="userAvatar" aria-label="User menu">
                    ${user.avatar || user.name.charAt(0).toUpperCase()}
                </button>
                <div class="dropdown-menu" id="userDropdown">
                    <div class="dropdown-header">
                        <div class="user-info">
                            <div class="user-name">${user.name}</div>
                            <div class="user-email">${user.email}</div>
                        </div>
                    </div>
                    <div class="dropdown-divider"></div>
                    <a href="#" class="dropdown-item" id="profileLink">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/>
                            <circle cx="12" cy="7" r="4"/>
                        </svg>
                        Profile
                    </a>
                    <a href="#" class="dropdown-item" id="savedCasesLink">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"/>
                        </svg>
                        Saved Cases
                    </a>
                    <a href="#" class="dropdown-item" id="settingsLink">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <circle cx="12" cy="12" r="3"/>
                            <path d="M12 1v6m0 6v6m11-7h-6m-6 0H1"/>
                        </svg>
                        Settings
                    </a>
                    <div class="dropdown-divider"></div>
                    <button class="dropdown-item logout-btn" id="logoutBtn">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>
                            <polyline points="16,17 21,12 16,7"/>
                            <line x1="21" y1="12" x2="9" y2="12"/>
                        </svg>
                        Log out
                    </button>
                </div>
            </div>
        `;

        // Add event listeners
        const userAvatar = document.getElementById('userAvatar');
        const userDropdown = document.getElementById('userDropdown');
        const logoutBtn = document.getElementById('logoutBtn');

        // Toggle dropdown
        if (userAvatar) {
            userAvatar.addEventListener('click', (e) => {
                e.stopPropagation();
                userDropdown.classList.toggle('show');
            });
        }

        // Close dropdown when clicking outside
        document.addEventListener('click', (e) => {
            if (!userAvatar?.contains(e.target) && !userDropdown?.contains(e.target)) {
                userDropdown?.classList.remove('show');
            }
        });

        // Handle logout
        if (logoutBtn) {
            logoutBtn.addEventListener('click', () => {
                auth.logout();
                updateNavbar();
                location.reload(); // Refresh to update navbar
            });
        }

        // Handle other dropdown items (placeholder functionality)
        document.getElementById('profileLink')?.addEventListener('click', (e) => {
            e.preventDefault();
            auth.showToast('Profile page coming soon!', 'info');
            userDropdown.classList.remove('show');
        });

        document.getElementById('savedCasesLink')?.addEventListener('click', (e) => {
            e.preventDefault();
            auth.showToast('Saved cases feature coming soon!', 'info');
            userDropdown.classList.remove('show');
        });

        document.getElementById('settingsLink')?.addEventListener('click', (e) => {
            e.preventDefault();
            auth.showToast('Settings page coming soon!', 'info');
            userDropdown.classList.remove('show');
        });
    } else {
        // User not authenticated, show login button
        navActions.innerHTML = `
            <button class="btn btn-outline" id="loginBtn">Log in</button>
        `;

        // Add login button event listener
        document.getElementById('loginBtn')?.addEventListener('click', () => {
            window.location.href = 'login.html';
        });
    }
}

// Auth guard function
function requireAuth(action) {
    if (auth.isAuthenticated()) {
        // User is authenticated, execute action
        if (action && typeof action === 'function') {
            action();
        }
    } else {
        // User not authenticated, show login modal
        showLoginModal(action);
    }
}

// Make functions globally available
window.requireAuth = requireAuth;
window.showLoginModal = showLoginModal;
window.updateNavbar = updateNavbar;