Test loading user from localstorage on first render

This commit is contained in:
James Grugett 2022-10-02 14:08:05 -05:00
parent 0ffd6c129a
commit 701d0a06cd

View File

@ -58,14 +58,9 @@ export function AuthProvider(props: {
serverUser?: AuthUser serverUser?: AuthUser
}) { }) {
const { children, serverUser } = props const { children, serverUser } = props
const [authUser, setAuthUser] = useStateCheckEquality<AuthUser>(serverUser) const [authUser, setAuthUser] = useStateCheckEquality<AuthUser>(
serverUser ?? getStoredUser()
useEffect(() => { )
if (serverUser === undefined) {
const cachedUser = localStorage.getItem(CACHED_USER_KEY)
setAuthUser(cachedUser && JSON.parse(cachedUser))
}
}, [setAuthUser, serverUser])
useEffect(() => { useEffect(() => {
if (authUser != null) { if (authUser != null) {
@ -134,3 +129,10 @@ export function AuthProvider(props: {
<AuthContext.Provider value={authUser}>{children}</AuthContext.Provider> <AuthContext.Provider value={authUser}>{children}</AuthContext.Provider>
) )
} }
const getStoredUser = () => {
if (typeof window === 'undefined') return undefined
const json = localStorage.getItem(CACHED_USER_KEY)
return json ? JSON.parse(json) : undefined
}