Hi everyone, I'm trying to find a way to persist my status values across page refreshes. Currently I'm storing the values in local storage and I've tried many solutions to keep the user logged in after the page reloads still doesn't work. I get this quit every time when I refresh the page, any help would be greatly appreciated. Thank you so much.
Core.ts:
class Core { agv: typeof agv; auth: Auth; workflow: WorkflowApi; graphql: GraphQLSdk; fleetState: Stream<FleetState>; taskList: Stream<TaskList>; zoneList: Stream<ZoneList>; configuration = getConfiguration(); url: string; constructor(options: Options) { const auth = getAuth(options); const graphqlUrl = `${options.host}/graphql`; const graphql = getSdk(new GraphQLClient(graphqlUrl, { fetch: authMiddleware({ auth, fetch: options.fetch }) })); const streamContext = { ...options, headers: getAuthHeaders(auth), getSites: () => auth.session.value?.sites || [], }; this.auth = auth; this.workflow = getWorkflowApi({ auth }) ; this.graphql = graphql; this.fleetState = fleetState.call({ ...options, headers: getAuthHeaders(auth), getSites: () => auth.session.value?.sites || [], }); this.configuration.map.fetch = this.configuration.map.fetch.bind(async () => { const site = auth.session.value.sites[0]; return graphql.map({ site }).then(({ map }) => map); }); this.taskList = taskList.call(streamContext); this.zoneList = zoneList.call(streamContext); this.agv = { ...agv, graphql, map: this.configuration.map, getSites: () => auth.session.value?.sites || [], } as typeof agv; auth.session.listen(session => { window.localStorage.setItem('session', JSON.stringify(session)); console.log(session); if (session) { this.configuration.map.fetch(); } }); } } export { Core };
If the session is persisted to localStorage, as follows
Then I think you can initialize from localStorage in
getAuth
as followsIf no session remains in the state,
null
is the value of the expression and is returned asinitialValue
.