@sebastian.scholl
Didn’t you say. 7 days? “You can keep doing this for up to 7-days.”
What I don’t have is a way to test it. I wrote some code, but I have no idea what the GraphQL response will be when the refreshToken is no longer valid, cause I haven’t been refreshing. the token for 3? (or hopefully 7 days) before it stops working.
Which means I can’t deploy my code, cause I don’t know with a 100% certainty that it will work.
and I don’t want all my users to not be able to login anymore (without clearing their cache) after 3 / 7 days.
I think I’ve covered the edge-cases required, but without knowing what the response will be when refreshToken
stops working, I can’t test it.
It also takes me 12 or 24 hours? to test the case when idToken stops working to make sure it can refresh it correctly.
Here’s the logic I have to figure out if I should send the user to the login page.
const { loading, error, data, refetch } = useQuery(CURRENT_USER_QUERY, { fetchPolicy: 'cache-and-network' })
const [refreshToken, { loading: refreshTokenLoading, data: refreshTokenData, called }] = useMutation(REFRESH_TOKEN)
// is fetching user or refreshing token
let authenticating = loading || refreshTokenLoading
const isLoginPage = Router.router.route.includes('/dashboard/login')
useEffect(() => {
// The user has no authToken saved in storage or window sess -> make user login
if (hasStorage && !localStorage.getItem('authToken')) {
if (isBrowser && !window.authToken) {
console.log('useEffect - dashboard push')
Router.push('/dashboard/login')
return
}
}
}, [])
if (error && !isLoginPage) {
// if there is an error - make the user login again and remove tokens
removeStoredTokens()
console.log('onError loginPush')
Router.push('/dashboard/login')
}
// if authToken is saved, but expired user.id will be null
if (data && data.user && data.user.id === null) {
// if refreshToken hasn't been attempted, try refreshing.
if (hasStorage && localStorage.refreshToken && !called) {
console.log('refreshToken - start')
refreshToken({ variables: { refreshToken: localStorage.refreshToken, authProfileId: AUTH_PROFILE_ID } })
} else {
// if refreshToken has been attempted and got a valid response
if (refreshTokenData && refreshTokenData.userRefreshToken) {
console.log('refreshToken - success')
storeTokens(refreshTokenData.userRefreshToken.idToken, refreshTokenData.userRefreshToken.refreshToken)
// refetch CURRENT_USER
refetch()
} else if (!authenticating) {
console.log('refreshToken - failed')
// if no auth requests are in progress and refreshToken data is invalid -> make user login
removeStoredTokens()
Router.push('/dashboard/login')
}
}
}
I need to test the following scenarios:
valid idToken & valid refreshToken:
expired idToken & valid refreshToken:
expired idToken & expired refreshToken:
Right now I can only test the first case. I have no way to test the second and third case without being able to invalidate (expire) my tokens. And there’s no docs that tells me what the response will be when it happens.