added pengguna,kurir page, with socket.io,need to switch this to node.js first

This commit is contained in:
kicap1992
2025-05-16 11:43:38 +08:00
parent 5329fb8265
commit 0ba4e846dc
1095 changed files with 12336 additions and 35 deletions

55
assets/auth.js Normal file
View File

@ -0,0 +1,55 @@
// check local storage
if (!localStorage.getItem('user')) {
// redirect to login page
window.location.href = "/";
}
const path = window.location.pathname;
const segments = path.split('/'); // ["", "kurir", "data"]
const target = segments[1];
// console.log(target);
if (target != 'kurir' && target != 'user') {
localStorage.removeItem('user');
localStorage.removeItem('role');
// redirect to login page
window.location.href = "/";
}
fetch(target == 'kurir' ? '/kurir/check' : '/user/check', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(JSON.parse(localStorage.getItem('user')))
})
.then(res => {
if (res.status >= 400) {
// remove local storage
localStorage.removeItem('user');
localStorage.removeItem('role');
// redirect to login page
window.location.href = "/";
}
// console.log(res);
return res.json();
})
.then(data => {
// console.log(data);
// document.getElementById('h5-title').innerHTML = data.username;
})
.catch(err => {
// console.log(err);
// remove local storage
localStorage.removeItem('user');
localStorage.removeItem('role');
// redirect to login page
window.location.href = "/";
});