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.
What?
Similar to the notFound() function and the
notFound.js
file, this PR follows the convention and introduces theforbidden()
function along side theforbiddden.js
file.Why?
While the available
notFound
andredirect
functions can get you a long way, there is only so much you can do with them.Developers need a way to handle the authorization state of a user across the multiple "contexts" next has, by that I am referring to components, api routes, and server actions. For this case the
403 Forbidden
status code can be used.(We would like to support 401 errors as well)
How?
As a guide, I used the existing implementation of
notFound
andredirect
.forbidden
files. In this case we display a default one.ForbiddenErrorBoundary
andForbidden
have been introduced in order to catch and display the 403 error.To verify that the changes are working the PR includes a minimal set of e2e tests.
How to use ?
Protect a page from a user who does not have the privilege to access it.
Protect an API route from a user who does not have the privilege to access it.
What's next
401 Unauthorized
status in a similar way.