Fixed issue with redirect after refresh-token expiration.

This commit is contained in:
Jesse Brault 2024-08-07 16:58:29 -05:00
parent 73cdf19ac6
commit 60c3ca4a3b

View File

@ -4,7 +4,7 @@ import {
QueryClientProvider QueryClientProvider
} from '@tanstack/react-query' } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools' import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import { useLocation, useNavigate } from '@tanstack/react-router' import { useRouter } from '@tanstack/react-router'
import React, { useState } from 'react' import React, { useState } from 'react'
import ExpiredTokenError from './api/ExpiredTokenError' import ExpiredTokenError from './api/ExpiredTokenError'
import refresh, { ExpiredRefreshTokenError } from './api/refresh' import refresh, { ExpiredRefreshTokenError } from './api/refresh'
@ -15,9 +15,8 @@ const AuthAwareQueryClientProvider = ({
children children
}: React.PropsWithChildren) => { }: React.PropsWithChildren) => {
const { putToken, clearToken } = useAuth() const { putToken, clearToken } = useAuth()
const navigate = useNavigate() const router = useRouter()
const [currentlyRefreshing, setCurrentlyRefreshing] = useState(false) const [currentlyRefreshing, setCurrentlyRefreshing] = useState(false)
const { href } = useLocation()
const doRefresh = async () => { const doRefresh = async () => {
if (!currentlyRefreshing) { if (!currentlyRefreshing) {
@ -31,11 +30,11 @@ const AuthAwareQueryClientProvider = ({
console.log('refresh-token expired') console.log('refresh-token expired')
setCurrentlyRefreshing(false) setCurrentlyRefreshing(false)
clearToken() clearToken()
await navigate({ await router.navigate({
to: '/login', to: '/login',
search: { search: {
expired: true, expired: true,
redirect: href redirect: router.state.location.href
} }
}) })
console.log('post-navigate') console.log('post-navigate')
@ -56,6 +55,13 @@ const AuthAwareQueryClientProvider = ({
new QueryClient({ new QueryClient({
defaultOptions: { defaultOptions: {
queries: { queries: {
retry(failureCount, error) {
if (error instanceof ExpiredTokenError) {
return false
} else {
return failureCount <= 3
}
},
retryDelay(failureCount, error) { retryDelay(failureCount, error) {
if (error instanceof ExpiredTokenError) { if (error instanceof ExpiredTokenError) {
return 0 return 0
@ -68,7 +74,6 @@ const AuthAwareQueryClientProvider = ({
queryCache: new QueryCache({ queryCache: new QueryCache({
onError(error) { onError(error) {
if (error instanceof ExpiredTokenError) { if (error instanceof ExpiredTokenError) {
console.error(error.message)
doRefresh() doRefresh()
} }
} }