feat(nextjs): Introduce clerkMiddleware - #2404
Merged
Merged
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 |
24 tasks
dimkl
approved these changes
Dec 19, 2023
| 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'); |
Contributor
There was a problem hiding this comment.
🙃 let's use the errorThrower instead of Error.
brkalow
reviewed
Dec 19, 2023
| switch (e.message) { | ||
| case PROTECT_REWRITE: | ||
| // Rewrite to bonus URL to force not found error | ||
| return NextResponse.rewrite(`${clerkRequest.clerkUrl.origin}/clerk_${Date.now()}`); |
Member
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.
nikosdouvlis
force-pushed
the
nikos/introduce-clerkmiddleware
branch
from
December 22, 2023 16:44
bb318b2 to
0939fda
Compare
nikosdouvlis
commented
Dec 22, 2023
Comment on lines
+110
to
+130
| 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); | ||
| // }; |
Member
Author
There was a problem hiding this comment.
Many thanks to @panteliselef for taking the time to investigate, test, and help me simplify these checks 🥇
Contributor
There was a problem hiding this comment.
Should we remove the commented out code?
nikosdouvlis
force-pushed
the
nikos/introduce-clerkmiddleware
branch
from
December 22, 2023 16:51
0939fda to
2ba6cc6
Compare
nikosdouvlis
force-pushed
the
nikos/introduce-clerkmiddleware
branch
from
January 3, 2024 01:12
2ba6cc6 to
00c6a30
Compare
nikosdouvlis
force-pushed
the
nikos/introduce-clerkmiddleware
branch
from
January 3, 2024 16:26
00c6a30 to
8b7c324
Compare
We'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
nikosdouvlis
force-pushed
the
nikos/introduce-clerkmiddleware
branch
from
January 3, 2024 17:12
8b7c324 to
832ca35
Compare
This was referenced Jun 21, 2026
This was referenced Jul 8, 2026
6 tasks
This was referenced Aug 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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