Skip to content

Commit

Permalink
Rewrite usePanoptesUser with useSWR
Browse files Browse the repository at this point in the history
- 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
eatyourgreens committed Oct 28, 2023
1 parent e1b6239 commit 8eefa1c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/app-root/src/helpers/fetchPanoptesUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { auth as authHelpers } from '@zooniverse/panoptes-js'
Get a Panoptes user from a Panoptes JSON Web Token (JWT), if we have one, or from
the Panoptes API otherwise.
*/
export default async function fetchPanoptesUser(storedUser) {
export default async function fetchPanoptesUser({ user: storedUser }) {
try {
const jwt = await auth.checkBearerToken()
/*
Expand Down
70 changes: 40 additions & 30 deletions packages/app-root/src/hooks/usePanoptesUser.js
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 }
}

0 comments on commit 8eefa1c

Please sign in to comment.