42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
export const user = $state({
|
|
name : null,
|
|
theme : 'default'
|
|
})
|
|
|
|
export async function checkUser(){
|
|
const url = `${location.protocol}//${location.host.replace('5173','8080')}/api/user/whoami`;
|
|
const response = await fetch(url,{
|
|
credentials: 'include'
|
|
});
|
|
if (response.ok){
|
|
const json = await response.json();
|
|
for (let key of Object.keys(json)) user[key] = json[key];
|
|
}
|
|
}
|
|
|
|
export async function logout(){
|
|
const url = `${location.protocol}//${location.host.replace('5173','8080')}/api/user/logout`;
|
|
await fetch(url,{
|
|
credentials: 'include'
|
|
});
|
|
user.name = null;
|
|
}
|
|
|
|
export async function tryLogin(credentials){
|
|
const url = `${location.protocol}//${location.host.replace('5173','8080')}/api/user/login`;
|
|
let response = await fetch(url,{
|
|
credentials: 'include',
|
|
headers: {
|
|
'Content-Type':'application/json'
|
|
},
|
|
method: 'POST',
|
|
body: JSON.stringify(credentials)
|
|
});
|
|
if (response.ok){
|
|
const json = await response.json();
|
|
for (let key of Object.keys(json)) user[key] = json[key];
|
|
|
|
} else {
|
|
alert("Login failed!");
|
|
}
|
|
} |