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; | ||||
| 			}); | ||||
| 		} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user