From 6316da53322360849873743087ed128fa9fbff39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Mon, 26 Dec 2022 14:32:58 +0100 Subject: [PATCH] fix auth state persistence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- app/soapbox/reducers/auth.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/soapbox/reducers/auth.ts b/app/soapbox/reducers/auth.ts index 5d4eb5636..47aadcb0a 100644 --- a/app/soapbox/reducers/auth.ts +++ b/app/soapbox/reducers/auth.ts @@ -75,8 +75,21 @@ const getSessionUser = () => { return validId(id) ? id : undefined; }; +const getLocalState = () => { + const state = JSON.parse(localStorage.getItem(STORAGE_KEY)!); + + if (!state) return undefined; + + return ReducerRecord({ + app: AuthAppRecord(state.app), + tokens: ImmutableMap(Object.entries(state.tokens).map(([key, value]) => [key, AuthTokenRecord(value as any)])), + users: ImmutableMap(Object.entries(state.users).map(([key, value]) => [key, AuthUserRecord(value as any)])), + me: state.me, + }); +}; + const sessionUser = getSessionUser(); -export const localState = fromJS(JSON.parse(localStorage.getItem(STORAGE_KEY)!)); +export const localState = getLocalState(); fromJS(JSON.parse(localStorage.getItem(STORAGE_KEY)!)); // Checks if the user has an ID and access token const validUser = (user?: AuthUser) => {