devportal2 / static /js /auth.js
Akay Borana
Fix terminal not loading: defer init until view is active
cdea1cc
Raw
History Blame Contribute Delete
3.76 kB
// static/js/auth.js v8
var currentToken = null, currentUser = '';
try { currentToken = localStorage.getItem('devportal_token'); currentUser = localStorage.getItem('devportal_user'); } catch (e) {}
window.onload = function() {
if (currentToken) {
document.getElementById('auth-screen').style.display = 'none';
document.getElementById('app-layout').classList.add('open');
// Default to terminal view
var termItem = document.querySelector('.nav-item[data-view=terminal-view]');
var termBnav = document.querySelector('.bnav-item[data-view=terminal-view]');
switchView('terminal-view', termItem);
if (termBnav) {
document.querySelectorAll('.bnav-item').forEach(function(i) { i.classList.remove('active'); });
termBnav.classList.add('active');
}
if (typeof initEditor === 'function') initEditor();
if (typeof initDashboard === 'function') initDashboard();
if (typeof loadSettings === 'function') loadSettings();
}
};
function toggleAuthMode(mode) {
var btn = document.querySelector('.auth-submit'), alt = document.querySelector('.auth-alt');
if (mode === 'register') {
btn.innerHTML = 'Create Account <i class="fa-solid fa-arrow-right"></i>';
btn.setAttribute('onclick', "handleAuth('register')");
alt.innerHTML = 'Have an account? <a onclick="toggleAuthMode(\'login\')">Sign In</a>';
} else {
btn.innerHTML = 'Sign In <i class="fa-solid fa-arrow-right"></i>';
btn.setAttribute('onclick', "handleAuth('login')");
alt.innerHTML = 'No account? <a onclick="toggleAuthMode(\'register\')">Create one</a>';
}
}
async function handleAuth(action) {
var errDiv = document.getElementById('auth-error');
errDiv.textContent = 'Authenticating...';
errDiv.style.color = 'var(--accent)';
try {
var u = document.getElementById('auth-username').value.trim();
var p = document.getElementById('auth-password').value.trim();
if (!u || !p) { errDiv.style.color = 'var(--error)'; errDiv.textContent = 'Fields cannot be empty'; return; }
var res = await fetch('/api/' + action, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({username: u, password: p}) });
var data = await res.json();
if (data.success) {
try { localStorage.setItem('devportal_token', data.token); localStorage.setItem('devportal_user', data.username); } catch (e) {}
currentToken = data.token; currentUser = data.username;
document.getElementById('auth-screen').style.display = 'none';
document.getElementById('app-layout').classList.add('open');
// Default to terminal view after login
var termItem = document.querySelector('.nav-item[data-view=terminal-view]');
var termBnav = document.querySelector('.bnav-item[data-view=terminal-view]');
switchView('terminal-view', termItem);
if (termBnav) {
document.querySelectorAll('.bnav-item').forEach(function(i) { i.classList.remove('active'); });
termBnav.classList.add('active');
}
if (typeof initEditor === 'function') initEditor();
if (typeof initDashboard === 'function') initDashboard();
if (typeof loadSettings === 'function') loadSettings();
} else { errDiv.style.color = 'var(--error)'; errDiv.textContent = data.error; }
} catch (e) { errDiv.style.color = 'var(--error)'; errDiv.textContent = 'Cannot reach server'; }
}
function logout() { try { localStorage.removeItem('devportal_token'); localStorage.removeItem('devportal_user'); } catch (e) {} location.reload(); }