parent
4ccd18ca9f
commit
d92c6c0de9
@ -17,7 +17,7 @@
|
||||
is_blocked_by_autologin = true;
|
||||
OpenAPI.TOKEN = value.access_token;
|
||||
const jwtinfo = JSON.parse(atob(OpenAPI.TOKEN.split(".")[1]));
|
||||
store.login(value.access_token, jwtinfo);
|
||||
store.login(value, jwtinfo);
|
||||
Toastify({
|
||||
text: $_("welcome_wavinghand"),
|
||||
duration: 500,
|
||||
|
29
src/store.js
29
src/store.js
@ -1,12 +1,15 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import { OpenAPI, AuthService } from '@odit/lfk-client-js';
|
||||
|
||||
export let users = writable([]);
|
||||
export let tracks = writable([]);
|
||||
const store = () => {
|
||||
const state = {
|
||||
auth: undefined,
|
||||
access_token: undefined,
|
||||
jwtinfo: undefined,
|
||||
isLoggedIn: false
|
||||
isLoggedIn: false,
|
||||
refreshInterval: undefined
|
||||
};
|
||||
|
||||
const { subscribe, set, update } = writable(state);
|
||||
@ -18,17 +21,37 @@ const store = () => {
|
||||
return state;
|
||||
});
|
||||
},
|
||||
login(access_token, jwtinfo) {
|
||||
refreshAuth() {
|
||||
console.log('refreshing auth');
|
||||
AuthService.authControllerRefresh({ token: state.auth.refresh_token }).then((auth) => {
|
||||
console.log('got new auth');
|
||||
console.log(auth);
|
||||
OpenAPI.TOKEN = auth.access_token;
|
||||
// TODO: update localstorage
|
||||
});
|
||||
},
|
||||
login(auth, jwtinfo) {
|
||||
update((state) => {
|
||||
state.access_token = access_token;
|
||||
state.auth = auth;
|
||||
state.access_token = auth.access_token;
|
||||
state.jwtinfo = jwtinfo;
|
||||
state.isLoggedIn = true;
|
||||
//
|
||||
state.refreshInterval = setInterval(() => {
|
||||
this.refreshAuth();
|
||||
// 4min
|
||||
}, 4 * 60000);
|
||||
//
|
||||
return state;
|
||||
});
|
||||
},
|
||||
logout() {
|
||||
update((state) => {
|
||||
state.isLoggedIn = false;
|
||||
//
|
||||
clearInterval(state.refreshInterval);
|
||||
state.refreshInterval = undefined;
|
||||
//
|
||||
return state;
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user