Skip to content

Commit

Permalink
caching token logic
Browse files Browse the repository at this point in the history
  • Loading branch information
papistacoding committed Jan 15, 2025
1 parent 3b58f26 commit ed244ed
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions apps/console/src/app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ const Providers = ({ children }: ProvidersProps) => {
const { data: session, status } = useSession()
const pathname = usePathname()
const [client, setClient] = useState<Client | null>(null)
const [accessToken, setAccessToken] = useState<string | null>(null)

useEffect(() => {
if (status === 'authenticated') {
const tokenChanged = session?.user.accessToken && session?.user.accessToken !== accessToken
if (status === 'authenticated' && tokenChanged) {
setAccessToken(session?.user.accessToken)
setClient(createClient(session))
} else if (status === 'unauthenticated' && pathname.endsWith('waitlist')) {
setClient(createSubscriberClient())
}
}, [session, status, pathname])
}, [session?.user.accessToken, status, pathname, accessToken])

if (status === 'loading' || (status === 'authenticated' && !client)) return null

Expand Down

0 comments on commit ed244ed

Please sign in to comment.