diff --git a/src/components/Login.svelte b/src/components/Login.svelte index 8fe56c8d..f2f12d86 100644 --- a/src/components/Login.svelte +++ b/src/components/Login.svelte @@ -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, diff --git a/src/store.js b/src/store.js index 44709628..8f8e5185 100644 --- a/src/store.js +++ b/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; }); }