Initial commit — Meridian OS browser desktop demo
This commit is contained in:
71
js/core/auth.js
Normal file
71
js/core/auth.js
Normal file
@@ -0,0 +1,71 @@
|
||||
import State from './state.js';
|
||||
import Bus from './bus.js';
|
||||
|
||||
const Auth = (() => {
|
||||
function login(name) {
|
||||
const trimmed = name.trim();
|
||||
if (!trimmed) return;
|
||||
const initials = trimmed.slice(0, 2).toUpperCase();
|
||||
State.set('user', { name: trimmed, initials });
|
||||
document.getElementById('login').classList.add('hidden');
|
||||
const desktop = document.getElementById('desktop');
|
||||
desktop.classList.remove('hidden');
|
||||
desktop.hidden = false;
|
||||
Bus.emit('user:login', { name: trimmed });
|
||||
// Persist login session
|
||||
localStorage.setItem('meridian:session', trimmed);
|
||||
}
|
||||
|
||||
function logout() {
|
||||
State.set('user', null);
|
||||
localStorage.removeItem('meridian:session');
|
||||
location.reload();
|
||||
}
|
||||
|
||||
function init() {
|
||||
const session = localStorage.getItem('meridian:session');
|
||||
if (session) {
|
||||
const trimmed = session.trim();
|
||||
if (trimmed) {
|
||||
const initials = trimmed.slice(0, 2).toUpperCase();
|
||||
State.set('user', { name: trimmed, initials });
|
||||
const loginEl = document.getElementById('login');
|
||||
if (loginEl) loginEl.classList.add('hidden');
|
||||
const desktop = document.getElementById('desktop');
|
||||
if (desktop) {
|
||||
desktop.classList.remove('hidden');
|
||||
desktop.hidden = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Wire login form
|
||||
const btn = document.getElementById('login-btn');
|
||||
const input = document.getElementById('login-input');
|
||||
const submit = () => { if (input && input.value.trim()) login(input.value); };
|
||||
if (btn) btn.addEventListener('click', submit);
|
||||
if (input) {
|
||||
input.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter') { e.preventDefault(); submit(); }
|
||||
});
|
||||
setTimeout(() => input.focus(), 50);
|
||||
}
|
||||
// Live clock on login screen
|
||||
const loginTime = document.getElementById('login-time');
|
||||
if (loginTime) {
|
||||
const tick = () => {
|
||||
const d = new Date();
|
||||
const hh = String(d.getHours()).padStart(2, '0');
|
||||
const mm = String(d.getMinutes()).padStart(2, '0');
|
||||
loginTime.textContent = `${hh}:${mm}`;
|
||||
};
|
||||
tick();
|
||||
setInterval(tick, 1000 * 30);
|
||||
}
|
||||
}
|
||||
|
||||
return { login, logout, init };
|
||||
})();
|
||||
|
||||
export default Auth;
|
||||
export { Auth };
|
||||
Reference in New Issue
Block a user