Added basic userstore
This commit is contained in:
parent
d13d6aa260
commit
5f70749e97
|
@ -28,7 +28,8 @@
|
|||
"type": "module",
|
||||
"dependencies": {
|
||||
"@sveltejs/adapter-static": "^1.0.0-next.16",
|
||||
"axios": "^0.21.1"
|
||||
"axios": "^0.21.1",
|
||||
"localforage": "^1.9.0"
|
||||
},
|
||||
"release-it": {
|
||||
"git": {
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
import { writable } from 'svelte/store';
|
||||
import localForage from 'localforage';
|
||||
|
||||
const userStore = () => {
|
||||
const state = {
|
||||
token: undefined,
|
||||
username: undefined,
|
||||
isLoggedIn: false,
|
||||
refreshInterval: undefined
|
||||
};
|
||||
|
||||
const { subscribe, set, update } = writable(state);
|
||||
|
||||
const methods = {
|
||||
init() {
|
||||
localForage.config({
|
||||
name: 'linkylinky',
|
||||
version: 1.0,
|
||||
storeName: 'linkylinky_dashboard',
|
||||
description: 'linkylinky Dashboard'
|
||||
});
|
||||
},
|
||||
login(authresponse) {
|
||||
update((state) => {
|
||||
state.token = authresponse.token;
|
||||
state.isLoggedIn = true;
|
||||
//
|
||||
localForage.setItem("userdata", state);
|
||||
//
|
||||
state.refreshInterval = setInterval(() => {
|
||||
//this.refreshAuth();
|
||||
// 2min
|
||||
}, 2 * 60000);
|
||||
//
|
||||
return state;
|
||||
});
|
||||
},
|
||||
logout() {
|
||||
update((state) => {
|
||||
state.isLoggedIn = false;
|
||||
state.token = undefined;
|
||||
//
|
||||
clearInterval(state.refreshInterval);
|
||||
state.refreshInterval = undefined;
|
||||
localForage.clear();
|
||||
//
|
||||
return state;
|
||||
});
|
||||
location.replace("/");
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
set,
|
||||
update,
|
||||
state,
|
||||
...methods
|
||||
};
|
||||
};
|
||||
export default userStore();
|
|
@ -1,6 +1,15 @@
|
|||
<script>
|
||||
import '../app.postcss';
|
||||
import Sidebar from '$lib/Sidebar.svelte';
|
||||
import UserStore from '$lib/UserStore';
|
||||
|
||||
$: isLoggedIn = false;
|
||||
|
||||
UserStore.init();
|
||||
const unsubscribe = UserStore.subscribe((value) => {
|
||||
isLoggedIn = value.isLoggedIn;
|
||||
});
|
||||
onDestroy(unsubscribe);
|
||||
</script>
|
||||
|
||||
<div style="min-height: 640px;" class="bg-white dark:bg-gray-800">
|
||||
|
|
19
yarn.lock
19
yarn.lock
|
@ -1654,6 +1654,11 @@ ignore@^5.1.4:
|
|||
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
|
||||
integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==
|
||||
|
||||
immediate@~3.0.5:
|
||||
version "3.0.6"
|
||||
resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"
|
||||
integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps=
|
||||
|
||||
import-cwd@3.0.0, import-cwd@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/import-cwd/-/import-cwd-3.0.0.tgz#20845547718015126ea9b3676b7592fb8bd4cf92"
|
||||
|
@ -1984,6 +1989,13 @@ levn@^0.4.1:
|
|||
prelude-ls "^1.2.1"
|
||||
type-check "~0.4.0"
|
||||
|
||||
lie@3.1.1:
|
||||
version "3.1.1"
|
||||
resolved "https://registry.yarnpkg.com/lie/-/lie-3.1.1.tgz#9a436b2cc7746ca59de7a41fa469b3efb76bd87e"
|
||||
integrity sha1-mkNrLMd0bKWd56QfpGmz77dr2H4=
|
||||
dependencies:
|
||||
immediate "~3.0.5"
|
||||
|
||||
lilconfig@^2.0.3:
|
||||
version "2.0.3"
|
||||
resolved "https://registry.yarnpkg.com/lilconfig/-/lilconfig-2.0.3.tgz#68f3005e921dafbd2a2afb48379986aa6d2579fd"
|
||||
|
@ -1994,6 +2006,13 @@ lines-and-columns@^1.1.6:
|
|||
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00"
|
||||
integrity sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA=
|
||||
|
||||
localforage@^1.9.0:
|
||||
version "1.9.0"
|
||||
resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.9.0.tgz#f3e4d32a8300b362b4634cc4e066d9d00d2f09d1"
|
||||
integrity sha512-rR1oyNrKulpe+VM9cYmcFn6tsHuokyVHFaCM3+osEmxaHTbEk8oQu6eGDfS6DQLWi/N67XRmB8ECG37OES368g==
|
||||
dependencies:
|
||||
lie "3.1.1"
|
||||
|
||||
lodash.clonedeep@^4.5.0:
|
||||
version "4.5.0"
|
||||
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
|
||||
|
|
Loading…
Reference in New Issue