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 23, 2023
1 parent 7bb4f9d commit 3bf2629
Show file tree
Hide file tree
Showing 2 changed files with 33 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
62 changes: 32 additions & 30 deletions packages/app-root/src/hooks/usePanoptesUser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import auth from 'panoptes-client/lib/auth'
import { useEffect, useState } from 'react'
import { useEffect } from 'react'
import useSWR from 'swr'

import { fetchPanoptesUser } from '../helpers'

Expand All @@ -11,46 +12,47 @@ if (isBrowser) {

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'
}

/*
`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)
if (data) {
storedUser = data
}

useEffect(function subscribeToAuthChanges() {
auth.listen('change', auth.checkCurrent)

return function () {
auth.stopListening('change', auth.checkCurrent)
}
}, [])

if (isBrowser) {
checkUserSession()
useEffect(function persistUserInStorage() {
if (data) {
localStorage?.setItem('panoptes-user', JSON.stringify(data))
}
auth.listen('change', checkUserSession)

return function () {
auth.stopListening('change', checkUserSession)
return () => {
localStorage?.removeItem('panoptes-user')
}
}, [])
}, [data])

return { data: user, error, isLoading: loading }
return { data: storedUser, error, isLoading }
}

0 comments on commit 3bf2629

Please sign in to comment.