Spaces:
Running
Running
File size: 5,683 Bytes
158d492 a30fd70 158d492 a30fd70 158d492 a30fd70 20fd4b7 17906e0 20fd4b7 158d492 a30fd70 158d492 20fd4b7 158d492 a30fd70 158d492 a30fd70 158d492 a30fd70 158d492 a30fd70 158d492 a30fd70 158d492 a30fd70 158d492 a30fd70 158d492 a30fd70 158d492 a30fd70 158d492 a30fd70 158d492 |
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 |
// ────────────────────────────── static/auth.js ──────────────────────────────
(function() {
const modal = document.getElementById('auth-modal');
const logoutBtn = document.getElementById('logout');
const userInfo = document.getElementById('user-info');
const userEmail = document.getElementById('user-email');
const themeToggle = document.getElementById('theme-toggle');
const tabs = document.querySelectorAll('.tab');
const tabLogin = document.getElementById('tab-login');
const tabSignup = document.getElementById('tab-signup');
const uploadSection = document.getElementById('upload-section');
const chatSection = document.getElementById('chat-section');
function setAuthUI(user) {
if (user && user.user_id) {
userInfo.style.display = 'flex';
userEmail.textContent = user.email;
// Enable app sections
uploadSection.style.display = 'block';
chatSection.style.display = 'block';
// Hide modal if it was open
modal.classList.add('hidden');
// Trigger project loading after successful auth
if (window.__sb_load_projects) {
window.__sb_load_projects();
// Update upload button after a short delay to ensure projects are loaded
setTimeout(() => {
if (window.__sb_update_upload_button) {
window.__sb_update_upload_button();
}
}, 100);
}
} else {
userInfo.style.display = 'none';
// Disable app sections for unauthenticated users
uploadSection.style.display = 'none';
chatSection.style.display = 'none';
// Show auth modal
showAuthModal();
}
}
function getUser() {
try {
return JSON.parse(localStorage.getItem('sb_user')) || null;
} catch { return null; }
}
function setUser(u) {
localStorage.setItem('sb_user', JSON.stringify(u));
setAuthUI(u);
}
function clearUser() {
localStorage.removeItem('sb_user');
localStorage.removeItem('sb_current_project');
setAuthUI(null);
}
function showAuthModal() {
modal.classList.remove('hidden');
modal.setAttribute('aria-hidden', 'false');
}
function hideAuthModal() {
modal.classList.add('hidden');
modal.setAttribute('aria-hidden', 'true');
}
// Initialize UI and check auth status
(function init() {
const user = getUser();
if (!user) {
// No user found, show modal immediately
showAuthModal();
} else {
setAuthUI(user);
}
})();
// Theme management
(function initTheme() {
const saved = localStorage.getItem('sb_theme');
if (saved === 'light') document.documentElement.classList.add('light');
themeToggle.textContent = document.documentElement.classList.contains('light') ? '☀️' : '🌙';
})();
themeToggle.addEventListener('click', () => {
document.documentElement.classList.toggle('light');
const isLight = document.documentElement.classList.contains('light');
localStorage.setItem('sb_theme', isLight ? 'light' : 'dark');
themeToggle.textContent = isLight ? '☀️' : '🌙';
});
// Logout
logoutBtn.addEventListener('click', () => {
clearUser();
showAuthModal();
});
// Tab switching
tabs.forEach(tab => tab.addEventListener('click', () => {
tabs.forEach(t => t.classList.remove('active'));
tab.classList.add('active');
const target = tab.dataset.tab;
if (target === 'login') {
tabLogin.classList.remove('hidden');
tabSignup.classList.add('hidden');
} else {
tabSignup.classList.remove('hidden');
tabLogin.classList.add('hidden');
}
}));
// Forms
document.getElementById('login-form').addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('login-email').value.trim();
const password = document.getElementById('login-password').value;
if (!email || !password) {
alert('Please fill in all fields');
return;
}
const fd = new FormData();
fd.append('email', email);
fd.append('password', password);
try {
const res = await fetch('/auth/login', { method: 'POST', body: fd });
if (!res.ok) {
const err = await res.json().catch(() => ({}));
alert(err.detail || 'Login failed');
return;
}
const data = await res.json();
setUser(data);
hideAuthModal();
} catch (error) {
alert('Network error. Please try again.');
}
});
document.getElementById('signup-form').addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('signup-email').value.trim();
const password = document.getElementById('signup-password').value;
if (!email || !password) {
alert('Please fill in all fields');
return;
}
if (password.length < 8) {
alert('Password must be at least 8 characters long');
return;
}
const fd = new FormData();
fd.append('email', email);
fd.append('password', password);
try {
const res = await fetch('/auth/signup', { method: 'POST', body: fd });
if (!res.ok) {
const err = await res.json().catch(() => ({}));
alert(err.detail || 'Signup failed');
return;
}
const data = await res.json();
setUser(data);
hideAuthModal();
} catch (error) {
alert('Network error. Please try again.');
}
});
// Expose helper to other scripts
window.__sb_get_user = getUser;
window.__sb_show_auth_modal = showAuthModal;
})();
|