mirror of
https://github.com/psforever/PSFPortal.git
synced 2026-07-16 08:55:14 +00:00
Initial commit
This commit is contained in:
commit
20946fdb43
49 changed files with 2393 additions and 0 deletions
52
app/UserState.js
Normal file
52
app/UserState.js
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import { writable } from 'svelte/store';
|
||||
import page from 'page';
|
||||
import axios from 'axios'
|
||||
|
||||
export const loggedIn = writable(false);
|
||||
export const username = writable("");
|
||||
export const isAdmin = writable(false);
|
||||
export const userId = writable(0);
|
||||
|
||||
function clear_user_state() {
|
||||
loggedIn.set(false)
|
||||
username.set("")
|
||||
isAdmin.set(false)
|
||||
userId.set(0)
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
try {
|
||||
await axios.post("/api/logout")
|
||||
} catch (e) {
|
||||
}
|
||||
|
||||
clear_user_state();
|
||||
page("/")
|
||||
}
|
||||
|
||||
loggedIn.subscribe((v) => {
|
||||
console.log(loggedIn, v)
|
||||
})
|
||||
|
||||
export async function get_initial_state() {
|
||||
try {
|
||||
const resp = await axios.get("/api/user")
|
||||
|
||||
loggedIn.set(true);
|
||||
username.set(resp.data.name);
|
||||
isAdmin.set(resp.data.admin);
|
||||
userId.set(resp.data.id);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
if (e.response.status === 403) {
|
||||
console.log("User not logged in / not admin!")
|
||||
clear_user_state();
|
||||
return false;
|
||||
} else {
|
||||
console.log("Unknown login error", e)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue