unauthorized
API Reference for the unauthorized function.
The unauthorized
function throws an error that renders a Next.js 401 error page. It's useful for handling authorization errors in your application. You can customize the UI using the unauthorized.js
file.
To start using unauthorized
, enable the experimental authInterrupts
configuration option in your next.config.js
file:
unauthorized
can be invoked in Server Components, Server Actions, and Route Handlers.
Good to know
- The
unauthorized
function cannot be called in the root layout.
Examples
Displaying login UI to unauthenticated users
You can use unauthorized
function to display the unauthorized.js
file with a login UI.
Mutations with Server Actions
You can invoke unauthorized
in Server Actions to ensure only authenticated users can perform specific mutations.
Fetching data with Route Handlers
You can use unauthorized
in Route Handlers to ensure only authenticated users can access the endpoint.
Version History
Version | Changes |
---|---|
v15.1.0 | unauthorized introduced. |