File size: 3,682 Bytes
16377f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fa171f
 
 
16377f2
995b87d
9fa171f
2b58527
16377f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fa171f
 
16377f2
 
995b87d
9fa171f
2b58527
16377f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
995b87d
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
// static/js/auth.js

let currentToken = null;
let currentUser = "";

// Safely check local storage
try {
    currentToken = localStorage.getItem("devportal_token");
    currentUser = localStorage.getItem("devportal_user");
} catch(e) {
    console.warn("Local storage is blocked.");
}

window.onload = () => {
    if(currentToken) {
        // Bypass login screen if already authenticated
        document.getElementById('auth-screen').style.display = 'none';
        document.getElementById('app-layout').style.display = 'flex';

        switchView('dashboard-view', document.querySelectorAll('.nav-item')[0]);

        if (typeof initTerminal === "function") initTerminal(); // Boot Terminal WebSocket
        if (typeof initEditor === "function") initEditor();
        if (typeof initDashboard === "function") initDashboard();
        if (typeof loadSettings === "function") loadSettings();
    }
};

function toggleAuthMode(mode) {
    const btn = document.querySelector('.auth-btn');
    const switchText = document.querySelector('.auth-switch');
    
    if(mode === 'register') {
        btn.innerHTML = `Create Account <span>→</span>`;
        btn.setAttribute('onclick', "handleAuth('register')");
        switchText.innerHTML = `Already have an account? <a onclick="toggleAuthMode('login')">Sign In</a>`;
    } else {
        btn.innerHTML = `Sign In <span>→</span>`;
        btn.setAttribute('onclick', "handleAuth('login')");
        switchText.innerHTML = `Don't have an account? <a onclick="toggleAuthMode('register')">Create Account</a>`;
    }
}

async function handleAuth(action) {
    const errDiv = document.getElementById('auth-error');
    errDiv.innerText = "Authenticating...";
    errDiv.style.color = "var(--accent-main)";

    try {
        const u = document.getElementById('auth-username').value.trim();
        const p = document.getElementById('auth-password').value.trim();
        
        if (!u || !p) {
            errDiv.style.color = "var(--error-color)";
            errDiv.innerText = "Credentials cannot be empty.";
            return;
        }

        const res = await fetch(`/api/${action}`, {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({username: u, password: p})
        });
        
        const 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;
            
            // Hide auth, show app
            document.getElementById('auth-screen').style.display = 'none';
            document.getElementById('app-layout').style.display = 'flex';

            switchView('dashboard-view', document.querySelectorAll('.nav-item')[0]);
            
            if (typeof initTerminal === "function") initTerminal();
            if (typeof initEditor === "function") initEditor();
            if (typeof initDashboard === "function") initDashboard();
            if (typeof loadSettings === "function") loadSettings();
        } else {
            errDiv.style.color = "var(--error-color)";
            errDiv.innerText = data.error;
        }
    } catch (err) {
        errDiv.style.color = "var(--error-color)";
        errDiv.innerText = "Network Error: Cannot reach server.";
    }
}

function logout() { 
    try { 
        localStorage.removeItem("devportal_token"); 
        localStorage.removeItem("devportal_user");
    } catch(e){} 
    location.reload(); 
}