-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use `useSWR` to fetch the user object from Panoptes. - persist the returned data in local storage. - reset the current Panoptes session when auth changes.
- Loading branch information
1 parent
588b804
commit 750b884
Showing
2 changed files
with
41 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,66 @@ | ||
import auth from 'panoptes-client/lib/auth' | ||
import { useEffect, useState } from 'react' | ||
import { useEffect } from 'react' | ||
import useSWR from 'swr' | ||
|
||
import { fetchPanoptesUser } from '../helpers' | ||
|
||
const isBrowser = typeof window !== 'undefined' | ||
|
||
const SWROptions = { | ||
revalidateIfStale: true, | ||
revalidateOnMount: true, | ||
revalidateOnFocus: true, | ||
revalidateOnReconnect: true, | ||
refreshInterval: 0 | ||
} | ||
|
||
if (isBrowser) { | ||
auth.checkCurrent() | ||
} | ||
|
||
const localStorage = isBrowser ? window.localStorage : null | ||
const storedUserJSON = localStorage?.getItem('panoptes-user') | ||
let user = storedUserJSON && JSON.parse(storedUserJSON) | ||
let storedUser = storedUserJSON && JSON.parse(storedUserJSON) | ||
/* | ||
Null users crash the ZooHeader component. | ||
Set them to undefined for now. | ||
*/ | ||
if (user === null) { | ||
user = undefined | ||
if (storedUser === null) { | ||
storedUser = undefined | ||
} | ||
|
||
export default function usePanoptesUser() { | ||
const [error, setError] = useState(null) | ||
const [loading, setLoading] = useState(true) | ||
|
||
useEffect(function () { | ||
async function checkUserSession() { | ||
setLoading(true) | ||
try { | ||
const panoptesUser = await fetchPanoptesUser(user) | ||
if (panoptesUser) { | ||
localStorage?.setItem('panoptes-user', JSON.stringify(panoptesUser)) | ||
user = panoptesUser | ||
} else { | ||
user = undefined | ||
localStorage?.removeItem('panoptes-user') | ||
} | ||
} catch (error) { | ||
setError(error) | ||
} | ||
setLoading(false) | ||
} | ||
const key = { | ||
user: storedUser, | ||
endpoint: '/me' | ||
} | ||
|
||
if (isBrowser) { | ||
checkUserSession() | ||
} | ||
auth.listen('change', checkUserSession) | ||
/* | ||
`useSWR` here will always return the same stale user object. | ||
See https://github.com/zooniverse/panoptes-javascript-client/issues/207 | ||
*/ | ||
const { data, error, isLoading } = useSWR(key, fetchPanoptesUser, SWROptions) | ||
if (data) { | ||
storedUser = data | ||
} | ||
|
||
useEffect(function subscribeToAuthChanges() { | ||
auth.listen('change', auth.checkCurrent) | ||
|
||
return function () { | ||
auth.stopListening('change', checkUserSession) | ||
auth.stopListening('change', auth.checkCurrent) | ||
} | ||
}, []) | ||
|
||
return { data: user, error, isLoading: loading } | ||
useEffect(function persistUserInStorage() { | ||
if (data) { | ||
localStorage?.setItem('panoptes-user', JSON.stringify(data)) | ||
} | ||
|
||
return () => { | ||
localStorage?.removeItem('panoptes-user') | ||
} | ||
}, [data]) | ||
|
||
return { data: storedUser, error, isLoading } | ||
} |