28 lines
608 B
JavaScript
28 lines
608 B
JavaScript
|
import { writable } from 'svelte/store';
|
||
|
|
||
|
function createUserStore() {
|
||
|
const { subscribe, set, update } = writable(undefined);
|
||
|
|
||
|
return {
|
||
|
subscribe,
|
||
|
set: (auth) => {
|
||
|
update((m) => auth);
|
||
|
},
|
||
|
update: (res_auth, cb=null) => {
|
||
|
if (res_auth.status === 200) {
|
||
|
res_auth.json().then((auth) => {
|
||
|
update((m) => (Object.assign(m?m:{}, auth)));
|
||
|
|
||
|
if (cb) {
|
||
|
cb(my);
|
||
|
}
|
||
|
});
|
||
|
} else if (res_auth.status >= 400 && res_auth.status < 500) {
|
||
|
update((m) => (null));
|
||
|
}
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export const user = createUserStore();
|