Replies: 9 comments 15 replies
-
A common pattern for handling unauthenticated access is to redirect the user to the sign-in page, and then make a successful sign-in redirect back to the original starting point. Can that pattern be replicated with |
Beta Was this translation helpful? Give feedback.
-
One thing that is still missing for me with Now that there are more pages with |
Beta Was this translation helpful? Give feedback.
-
Probably a good idea to also document on how streaming ( |
Beta Was this translation helpful? Give feedback.
-
So this is very nice and long awaited (no pun intended), but I'm wondering why we're sugaring over status codes instead of just letting us I get the advantage of custom error pages (unauthorized.tsx), but we're adding cruft to web standards by not just leaning on them and creating our own language. If we can put in any status code, we can do Can you explain the reasoning behind not just opening up any status code to be sent? |
Beta Was this translation helpful? Give feedback.
-
Maybe the possibility of passing down a payload to the unauthorized / forbidden page? e.g. for info on which scope a user is missing. Also a retry/reset function similar to error pages could be nice for forbidden, in case a colleague added the scope a few seconds later? |
Beta Was this translation helpful? Give feedback.
-
Is it possible to globally check whether a user is authenticated and invoke unauthorized() without having to implement the check on every individual route or page? Based on the current documentation, it seems necessary to perform this authentication check separately for each route. Is there a more efficient method or best practice for handling authentication across the entire application? Thank you for your assistance! |
Beta Was this translation helpful? Give feedback.
-
I'd like to suggest also exposing the Errors that are thrown by these methods or some function that can "identify" them. The reason is that these control flows are realised via JS errors that are thrown and sometimes in a catch block, you need to differentiate if it's a not found/unauthorized/forbidden error and then re-throw it while handling other application errors. It seems like library authors like for next-safe-action are reaching very deep into undocumented files to make this happen right now: https://github.com/TheEdoRan/next-safe-action/blob/86f00f422c6799e81c57d402dfc40354cf78db17/packages/next-safe-action/src/utils.ts#L5 |
Beta Was this translation helpful? Give feedback.
-
I wonder... why instead of making specific functions for “popular” http response types, better a generic function that allows us to respond with any type of http response? |
Beta Was this translation helpful? Give feedback.
-
Although they are useful, I wonder if it is appropriate to use them in server components. For me it makes more sense from an earlier stage, the middleware, to check this and return the Response if necessary with the appropriate status. In the end if you are already rendering the page in streaming the headers have already been sent and using these methods cause a soft-redirect from the client, where the status is always 200, which is fine to give feedback to the users, but I have my doubts if it is good practice. |
Beta Was this translation helpful? Give feedback.
-
Overview
If you’re familiar with the App Router, you’ve likely used
notFound()
to trigger 404 behavior alongside the customizablenot-found.tsx
file. In Next 15, we're actively exploring extending this approach to authorization errors:•
forbidden()
triggers a 403 error with customizable UI viaforbidden.tsx
.•
unauthorized()
triggers a 401 error with customizable UI viaunauthorized.tsx
.Good to know: As with
notFound()
errors, the status code will be a 200 if the error is triggered after initial response headers have been sent. Learn more.Enabling the feature
As this feature is still experimental, you’ll need to enable it in your
next.config.js
file:Using
forbidden()
andunauthorized()
You can use
forbidden()
andunauthorized()
in Server Actions, Server Components, Client Components, or Route Handlers. Here’s an example:Creating custom error pages
To customize the error pages, create the following files:
You can read more about the APIs in the forbidden and unauthorized documentation.
Note: before we stabilize this feature for 15.2, we're planning on adding more capabilities and improvements to the APIs to support a wider range of use cases.
We're actively iterating on this API and would love to hear your feedback about how it could be improved. Please feel free to leave any comments/suggestions/concerns in this thread. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions