🔒 added basic manual refresh every 4mins

ref #29
This commit is contained in:
Philipp Dormann 2021-01-10 13:03:48 +01:00
parent 4ccd18ca9f
commit d92c6c0de9
2 changed files with 27 additions and 4 deletions

View File

@ -17,7 +17,7 @@
is_blocked_by_autologin = true; is_blocked_by_autologin = true;
OpenAPI.TOKEN = value.access_token; OpenAPI.TOKEN = value.access_token;
const jwtinfo = JSON.parse(atob(OpenAPI.TOKEN.split(".")[1])); const jwtinfo = JSON.parse(atob(OpenAPI.TOKEN.split(".")[1]));
store.login(value.access_token, jwtinfo); store.login(value, jwtinfo);
Toastify({ Toastify({
text: $_("welcome_wavinghand"), text: $_("welcome_wavinghand"),
duration: 500, duration: 500,

View File

@ -1,12 +1,15 @@
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
import { OpenAPI, AuthService } from '@odit/lfk-client-js';
export let users = writable([]); export let users = writable([]);
export let tracks = writable([]); export let tracks = writable([]);
const store = () => { const store = () => {
const state = { const state = {
auth: undefined,
access_token: undefined, access_token: undefined,
jwtinfo: undefined, jwtinfo: undefined,
isLoggedIn: false isLoggedIn: false,
refreshInterval: undefined
}; };
const { subscribe, set, update } = writable(state); const { subscribe, set, update } = writable(state);
@ -18,17 +21,37 @@ const store = () => {
return state; 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) => { update((state) => {
state.access_token = access_token; state.auth = auth;
state.access_token = auth.access_token;
state.jwtinfo = jwtinfo; state.jwtinfo = jwtinfo;
state.isLoggedIn = true; state.isLoggedIn = true;
//
state.refreshInterval = setInterval(() => {
this.refreshAuth();
// 4min
}, 4 * 60000);
//
return state; return state;
}); });
}, },
logout() { logout() {
update((state) => { update((state) => {
state.isLoggedIn = false; state.isLoggedIn = false;
//
clearInterval(state.refreshInterval);
state.refreshInterval = undefined;
//
return state; return state;
}); });
} }