-
Notifications
You must be signed in to change notification settings - Fork 318
Description
Describe the bug
The onError
callback in the GraphQL client is designed to detect errors of the BigCommerceAuthError
type and redirect to the auth signout route. This ensures that if a user's session is invalid we trigger a full sign out to clear any user data, and redirect to the login page.
onError: (error, queryType) => {
if (error instanceof BigCommerceAuthError && queryType === 'query') {
redirect('/api/auth/signout');
}
}
This works great for simple functions like getCart
. However, we have noticed that if the query/mutation to BC is inside a try/catch, we instead get a NEXT_REDIRECT
error.
I believe this is actually expected behaviour from Next. NextJS Redirect Behaviour
redirect throws an error so it should be called outside the try block when using try/catch statements.
In the case of the addToCart
function, the call to BC is in a try block. The GraphQL client will get the BigCommerceAuthError
and trigger the redirect. However, the redirect will throw an error, which is then caught by the catch
block, preventing completion of the redirect.
To Reproduce
Steps to reproduce the behavior:
- Sign in to Catalyst storefront
- Open Catalyst in a private window or another device and sign in again.
- This will invalidate the first session due to BC only allowing individual sessions.
- On the first window/device, try adding an item to the cart.
- See error
Expected behavior
The expectation is that for any query/mutation to BC, the automatic redirect to the signout route should be successful.
Screenshots
