-
Notifications
You must be signed in to change notification settings - Fork 436
fix(nextjs): return 401 for unauthenticated server action requests #7820
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: cab1547 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 GitHub.
|
@clerk/agent-toolkit
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/dev-cli
@clerk/expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/hono
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/react
@clerk/react-router
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/ui
@clerk/upgrade
@clerk/vue
commit: |
wobsoriano
left a comment
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.
nicee
📝 WalkthroughWalkthroughThis pull request introduces a breaking change in the Clerk Next.js package where unauthenticated server action requests handled by auth.protect() now return HTTP 401 (Unauthorized) instead of 404 (Not Found). The implementation detects server action requests via specific request headers and routes them to an unauthorized response. A test case validates this behavior, and a changeset documents the major version bump. Upgrade documentation provides migration guidance for client-side error handling that depends on the response status. 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. No actionable comments were generated in the recent review. 🎉 Comment |
Summary
auth.protect()in middleware now returns 401 (Unauthorized) instead of 404 (Not Found) when the user is unauthenticated and the request is a server actionisServerActionRequest()helper (checksnext-url+next-action/text/x-component/multipart/form-dataheaders)How it works
Added a check for server action requests in the
handleUnauthenticatedclosure inprotect.ts, between the existingisPageRequestcheck and thenotFound()fallback:The
unauthorizedcallback is already passed intocreateProtectby both callers (middleware and server components), so the existing 401 error handling path works without any other changes.Test plan
notFound()pnpm build --filter=@clerk/nextjspassespnpm vitest run packages/nextjs/src/server/__tests__/clerkMiddleware.test.tspasses (104/104)Summary by CodeRabbit
Breaking Changes
auth.protect()now returns 401 Unauthorized instead of 404 Not Found for unauthenticated server action requestsDocumentation