Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib-classifier): refresh expired bearer tokens #6604

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/lib-classifier/src/hooks/usePanoptesAuth.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { useEffect, useState } from 'react'
import { useState } from 'react'

import { getBearerToken } from '@store/utils'

export default function usePanoptesAuth({ authClient, userID }) {
export default function usePanoptesAuth({ authClient }) {
const [authorization, setAuthorization] = useState()

async function checkAuth() {
const token = await getBearerToken(authClient)
setAuthorization(token)
if (token !== authorization) {
setAuthorization(token)
}
}

useEffect(function onUserChange() {
checkAuth()
}, [authClient, userID])
Comment on lines -12 to -14
Copy link
Contributor Author

@eatyourgreens eatyourgreens Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original bug happens because this hook stores your bearer token in component state and reuses it forever, even after it has expired. The token should be checked (by running checkAuth()) every time that it's used.

Copy link
Contributor Author

@eatyourgreens eatyourgreens Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

General tip for reviewing React components: whenever you see useEffect it has probably introduced a subtle bug.

checkAuth()

return authorization
}