35 lines
831 B
JavaScript
35 lines
831 B
JavaScript
|
|
import { auth } from './services/auth.js';
|
||
|
|
import { renderLoginPage } from './pages/LoginPage.js';
|
||
|
|
import { renderDashboard } from './pages/DashboardPage.js';
|
||
|
|
|
||
|
|
export function initRouter() {
|
||
|
|
const app = document.querySelector('#app');
|
||
|
|
|
||
|
|
function navigate() {
|
||
|
|
const hash = window.location.hash || '#login';
|
||
|
|
|
||
|
|
if (!auth.checkAuth() && hash !== '#login') {
|
||
|
|
window.location.hash = '#login';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
app.innerHTML = '';
|
||
|
|
|
||
|
|
switch (hash) {
|
||
|
|
case '#login':
|
||
|
|
app.appendChild(renderLoginPage(() => {
|
||
|
|
window.location.hash = '#dashboard';
|
||
|
|
}));
|
||
|
|
break;
|
||
|
|
case '#dashboard':
|
||
|
|
app.appendChild(renderDashboard());
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
window.location.hash = '#login';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
window.addEventListener('hashchange', navigate);
|
||
|
|
navigate();
|
||
|
|
}
|