Spaces:
Sleeping
Sleeping
Create static/js/auth.js
Browse files- static/js/auth.js +91 -0
static/js/auth.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// static/js/auth.js
|
| 2 |
+
|
| 3 |
+
let currentToken = null;
|
| 4 |
+
let currentUser = "";
|
| 5 |
+
|
| 6 |
+
// Safely check local storage
|
| 7 |
+
try {
|
| 8 |
+
currentToken = localStorage.getItem("devportal_token");
|
| 9 |
+
currentUser = localStorage.getItem("devportal_user");
|
| 10 |
+
} catch(e) {
|
| 11 |
+
console.warn("Local storage is blocked.");
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
window.onload = () => {
|
| 15 |
+
if(currentToken) {
|
| 16 |
+
// Bypass login screen if already authenticated
|
| 17 |
+
document.getElementById('auth-screen').style.display = 'none';
|
| 18 |
+
document.getElementById('app-layout').style.display = 'flex';
|
| 19 |
+
if (typeof initTerminal === "function") initTerminal(); // Boot Terminal WebSocket
|
| 20 |
+
}
|
| 21 |
+
};
|
| 22 |
+
|
| 23 |
+
function toggleAuthMode(mode) {
|
| 24 |
+
const btn = document.querySelector('.auth-btn');
|
| 25 |
+
const switchText = document.querySelector('.auth-switch');
|
| 26 |
+
|
| 27 |
+
if(mode === 'register') {
|
| 28 |
+
btn.innerHTML = `Create Account <span>→</span>`;
|
| 29 |
+
btn.setAttribute('onclick', "handleAuth('register')");
|
| 30 |
+
switchText.innerHTML = `Already have an account? <a onclick="toggleAuthMode('login')">Sign In</a>`;
|
| 31 |
+
} else {
|
| 32 |
+
btn.innerHTML = `Sign In <span>→</span>`;
|
| 33 |
+
btn.setAttribute('onclick', "handleAuth('login')");
|
| 34 |
+
switchText.innerHTML = `Don't have an account? <a onclick="toggleAuthMode('register')">Create Account</a>`;
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
async function handleAuth(action) {
|
| 39 |
+
const errDiv = document.getElementById('auth-error');
|
| 40 |
+
errDiv.innerText = "Authenticating...";
|
| 41 |
+
errDiv.style.color = "var(--accent-main)";
|
| 42 |
+
|
| 43 |
+
try {
|
| 44 |
+
const u = document.getElementById('auth-username').value.trim();
|
| 45 |
+
const p = document.getElementById('auth-password').value.trim();
|
| 46 |
+
|
| 47 |
+
if (!u || !p) {
|
| 48 |
+
errDiv.style.color = "var(--error-color)";
|
| 49 |
+
errDiv.innerText = "Credentials cannot be empty.";
|
| 50 |
+
return;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
const res = await fetch(`/api/${action}`, {
|
| 54 |
+
method: 'POST',
|
| 55 |
+
headers: {'Content-Type': 'application/json'},
|
| 56 |
+
body: JSON.stringify({username: u, password: p})
|
| 57 |
+
});
|
| 58 |
+
|
| 59 |
+
const data = await res.json();
|
| 60 |
+
|
| 61 |
+
if(data.success) {
|
| 62 |
+
try {
|
| 63 |
+
localStorage.setItem("devportal_token", data.token);
|
| 64 |
+
localStorage.setItem("devportal_user", data.username);
|
| 65 |
+
} catch(e) {}
|
| 66 |
+
|
| 67 |
+
currentToken = data.token;
|
| 68 |
+
currentUser = data.username;
|
| 69 |
+
|
| 70 |
+
// Hide auth, show app
|
| 71 |
+
document.getElementById('auth-screen').style.display = 'none';
|
| 72 |
+
document.getElementById('app-layout').style.display = 'flex';
|
| 73 |
+
|
| 74 |
+
if (typeof initTerminal === "function") initTerminal();
|
| 75 |
+
} else {
|
| 76 |
+
errDiv.style.color = "var(--error-color)";
|
| 77 |
+
errDiv.innerText = data.error;
|
| 78 |
+
}
|
| 79 |
+
} catch (err) {
|
| 80 |
+
errDiv.style.color = "var(--error-color)";
|
| 81 |
+
errDiv.innerText = "Network Error: Cannot reach server.";
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
function logout() {
|
| 86 |
+
try {
|
| 87 |
+
localStorage.removeItem("devportal_token");
|
| 88 |
+
localStorage.removeItem("devportal_user");
|
| 89 |
+
} catch(e){}
|
| 90 |
+
location.reload();
|
| 91 |
+
}
|