Uh oh!
There was an error while loading. Please reload this page.
feat(nextjs): Introduce clerkMiddleware - #2404
Conversation
🦋 Changeset detectedLatest commit: 832ca35 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 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| const res = new Response(null, { status: 307, headers: requestState.headers }); | ||
| return decorateResponseWithObservabilityHeaders(res, requestState); | ||
| } else if (requestState.status === AuthStatus.Handshake) { | ||
| throw new Error('Clerk: handshake status without redirect'); |
There was a problem hiding this comment.
🙃 let's use the errorThrower instead of Error.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| switch (e.message) { | ||
| case PROTECT_REWRITE: | ||
| // Rewrite to bonus URL to force not found error | ||
| return NextResponse.rewrite(`${clerkRequest.clerkUrl.origin}/clerk_${Date.now()}`); |
There was a problem hiding this comment.
🤔 this might pollute a consumer's logs, it's probably fine but we should call this out in the docs.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
bb318b2 to
0939fdaCompare| const isServerActionRequest = (req: Request) => { | ||
| return ( | ||
| !!req.headers.get(nextConstants.Headers.NextUrl) && | ||
| (req.headers.get(constants.Headers.Accept)?.includes('text/x-component') || | ||
| req.headers.get(constants.Headers.ContentType)?.includes('multipart/form-data') || | ||
| !!req.headers.get(nextConstants.Headers.NextAction)) | ||
| ); | ||
| }; | ||
| const isPageRequest = (req: Request): boolean => { | ||
| return ( | ||
| req.headers.get(constants.Headers.SecFetchDest) === 'document' || | ||
| req.headers.get(constants.Headers.Accept)?.includes('text/html') || | ||
| (!!req.headers.get(nextConstants.Headers.NextUrl) && !isServerActionRequest(req)) | ||
| ); | ||
| }; | ||
| // In case we want to handle router handlers and server actions differently in the future | ||
| // const isRouteHandler = (req: Request) => { | ||
| // return !isPageRequest(req) && !isServerAction(req); | ||
| // }; |
There was a problem hiding this comment.
Many thanks to @panteliselef for taking the time to investigate, test, and help me simplify these checks 🥇
There was a problem hiding this comment.
Should we remove the commented out code?
0939fda to
2ba6cc6Compare2ba6cc6 to
00c6a30Compare00c6a30 to
8b7c324CompareWe're leveraging the isPageRequest and isServerActionRequest checks to detect whether the current request is a page request. For page requests, instead of throwing a 404 or 401, we're redirecting to SIGN_IN_URL automatically. In order to achieve that, we're using native headers but also the `next-urk` and `next-action` headers of NextJS
8b7c324 to
832ca35Compare
Description
clerkMiddlewareAPI:protectAPI:The
protectAPI can be used to protect pages, route handlers, server actions and any other request that can be intercepted using the middleware. It can be used to handle both unauthenticated and unauthorized requests.Calling
protectwithout arguments will catch all unauthenticated requests, and depending on the context it's used in, it will either redirect to the sign-in URL (for page requests) or throw anotFounderror (for route handlers and server actions). If aredirectUrlis provided,protectwill always respect it and redirect to it.Calling
protectwith the same arguments thehasAPI accepts, will catch all unauthorized requests, and throw anotFounderror (for route handlers and server actions). If aredirectUrlis provided,protectwill always respect it and redirect to it.SDK-647
Checklist
npm testruns as expected.npm run buildruns as expected.Type of change
Packages affected
@clerk/backend@clerk/chrome-extension@clerk/clerk-js@clerk/clerk-expo@clerk/fastifygatsby-plugin-clerk@clerk/localizations@clerk/nextjs@clerk/clerk-react@clerk/remix@clerk/clerk-sdk-node@clerk/shared@clerk/themes@clerk/typesbuild/tooling/chore