-
Notifications
You must be signed in to change notification settings - Fork 307
Update GQL client and auth middleware to handle invalid tokens and invalidate session #2259
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
Conversation
🦋 Changeset detectedLatest commit: 8e7a907 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
4 Skipped Deployments
|
export const withAuth: MiddlewareFactory = (next) => { | ||
return async (request, event) => { | ||
// @ts-expect-error: The `auth` function doesn't have the correct type to support it as a MiddlewareFactory. | ||
const authWithCallback = auth(async (req) => { | ||
const isProtectedRoute = protectedPathRegExp.test(req.nextUrl.pathname); | ||
const isGetRequest = req.method === 'GET'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only redirect on GET
requests to avoid breaking any server actions.
core/app/[locale]/(default)/account/addresses/_actions/create-address.ts
Outdated
Show resolved
Hide resolved
46ea5c9
to
22d577c
Compare
22d577c
to
1225853
Compare
// eslint-disable-next-line @typescript-eslint/no-restricted-imports | ||
import { redirect } from 'next/navigation'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using next/navigation
here for a few reasons:
- For some reason importing
~/i18n/routing
breaks this file and the build starts failing. (Module not found error) - We don't need locale info for the session invalidation redirect, as it's just redirecting to the same route with the
?invalidate-session
query param.
@@ -74,6 +85,7 @@ class Client<FetcherRequestInit extends RequestInit = RequestInit> { | |||
fetchOptions?: FetcherRequestInit; | |||
channelId?: string; | |||
errorPolicy?: 'none' | 'all' | 'ignore'; | |||
validateCustomerAccessToken?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why this is optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jorgemoya Yep, it's optional so that we don't have to update every single client.fetch
in Catalyst to get this behavior. It's optional, but defaults to true
, so if we want to override the behavior somewhere, we can.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I guess my question was more in the sense of why would we want to override it but having the optional override shouldn't hurt.
onError?: ( | ||
error: BigCommerceGQLError, | ||
queryType: 'query' | 'mutation' | 'subscription', | ||
) => Promise<void> | void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an existing pattern or something we built for this use case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's built for this use-case. As @chanceaclark was saying, we need the auth failure handling to be part of the DAL, which will require having this onError
handler built into the client. 👍
core/app/[locale]/(default)/account/wishlists/_actions/change-wishlist-visibility.ts
Show resolved
Hide resolved
f5c42c1
to
648cfac
Compare
90cd2da
to
0152752
Compare
⚡️🏠 Lighthouse reportLighthouse ran against https://catalyst-canary-et08lsoyd-bigcommerce-platform.vercel.app 🖥️ DesktopWe ran Lighthouse against the changes on a desktop and produced this report. Here's the summary:
📱 MobileWe ran Lighthouse against the changes on a mobile and produced this report. Here's the summary:
|
What/Why?
withAuth
middleware to redirect protected routes to/login
page when user is unauthorizedonError
event to GraphQL clientonError
callback to invalidate user session when GQL returns an invalid or missing token errorTesting
Screen.Recording.2025-05-02.at.5.54.09.PM.mov
Migration
/core/client
directory and the/packages/client
directory?invalidate-session
query param/core/app/[locale]/(default)/account/
directory server actions/core/app/[locale]/(default)/checkout/route.ts
file