Changeset 0.22.6 (#148)
This commit is contained in:
@@ -205,6 +205,51 @@ const Pages = {
|
||||
if (ok) _toast('Settings saved', 'success');
|
||||
},
|
||||
|
||||
// ── Login ─────────────────────────────────
|
||||
|
||||
async doLogin() {
|
||||
const username = _val('loginUsername');
|
||||
const password = _val('loginPassword');
|
||||
const errEl = document.getElementById('loginError');
|
||||
const btn = document.getElementById('loginBtn');
|
||||
if (!username || !password) {
|
||||
if (errEl) { errEl.textContent = 'Enter username and password'; errEl.style.display = ''; }
|
||||
return;
|
||||
}
|
||||
if (errEl) errEl.style.display = 'none';
|
||||
if (btn) { btn.disabled = true; btn.textContent = 'Logging in…'; }
|
||||
|
||||
const base = window.__BASE__ || '';
|
||||
try {
|
||||
const resp = await fetch(base + '/api/v1/auth/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ login: username, password }),
|
||||
});
|
||||
if (!resp.ok) {
|
||||
const err = await resp.json().catch(() => ({}));
|
||||
throw new Error(err.error || `Login failed (${resp.status})`);
|
||||
}
|
||||
const data = await resp.json();
|
||||
|
||||
// Save tokens in same format as API._setAuth / API.saveTokens
|
||||
const storageKey = base ? `sb_auth_${base.replace(/\//g, '')}` : 'sb_auth';
|
||||
localStorage.setItem(storageKey, JSON.stringify({
|
||||
accessToken: data.access_token,
|
||||
refreshToken: data.refresh_token,
|
||||
user: data.user,
|
||||
}));
|
||||
// Set cookie for server-rendered page auth
|
||||
document.cookie = `sb_token=${data.access_token}; path=/; max-age=900; SameSite=Strict`;
|
||||
|
||||
// Redirect to chat surface
|
||||
window.location.href = base + '/';
|
||||
} catch (e) {
|
||||
if (errEl) { errEl.textContent = e.message; errEl.style.display = ''; }
|
||||
if (btn) { btn.disabled = false; btn.textContent = 'Log In'; }
|
||||
}
|
||||
},
|
||||
|
||||
// ── User Settings (settings surface) ─────
|
||||
|
||||
async saveProfile() {
|
||||
|
||||
Reference in New Issue
Block a user