Download next.js/docs/01-app/03-api-reference/03-file-conventions/unauthorized.mdx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/docs/01-app/03-api-reference/03-file-conventions/unauthorized.mdx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/docs/01-app/03-api-reference/03-file-conventions/unauthorized.mdx
-
curl -L -o unauthorized.mdx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/docs/01-app/03-api-reference/03-file-conventions/unauthorized.mdx
2.61 kB
| --- | |
| title: unauthorized.js | |
| description: API reference for the unauthorized.js special file. | |
| related: | |
| links: | |
| - app/api-reference/functions/unauthorized | |
| version: experimental | |
| --- | |
| The **unauthorized** file is used to render UI when the [`unauthorized`](/docs/app/api-reference/functions/unauthorized) function is invoked during authentication. Along with allowing you to customize the UI, Next.js will return a `401` status code. | |
| ```tsx filename="app/unauthorized.tsx" switcher | |
| import Login from '@/app/components/Login' | |
| export default function Unauthorized() { | |
| return ( | |
| <main> | |
| <h1>401 - Unauthorized</h1> | |
| <p>Please log in to access this page.</p> | |
| <Login /> | |
| </main> | |
| ) | |
| } | |
| ``` | |
| ```jsx filename="app/unauthorized.js" switcher | |
| import Login from '@/app/components/Login' | |
| export default function Unauthorized() { | |
| return ( | |
| <main> | |
| <h1>401 - Unauthorized</h1> | |
| <p>Please log in to access this page.</p> | |
| <Login /> | |
| </main> | |
| ) | |
| } | |
| ``` | |
| ## Reference | |
| ### Props | |
| `unauthorized.js` components do not accept any props. | |
| ## Examples | |
| ### Displaying login UI to unauthenticated users | |
| You can use [`unauthorized`](/docs/app/api-reference/functions/unauthorized) function to render the `unauthorized.js` file with a login UI. | |
| ```tsx filename="app/dashboard/page.tsx" switcher | |
| import { verifySession } from '@/app/lib/dal' | |
| import { unauthorized } from 'next/navigation' | |
| export default async function DashboardPage() { | |
| const session = await verifySession() | |
| if (!session) { | |
| unauthorized() | |
| } | |
| return <div>Dashboard</div> | |
| } | |
| ``` | |
| ```jsx filename="app/dashboard/page.js" switcher | |
| import { verifySession } from '@/app/lib/dal' | |
| import { unauthorized } from 'next/navigation' | |
| export default async function DashboardPage() { | |
| const session = await verifySession() | |
| if (!session) { | |
| unauthorized() | |
| } | |
| return <div>Dashboard</div> | |
| } | |
| ``` | |
| ```tsx filename="app/unauthorized.tsx" switcher | |
| import Login from '@/app/components/Login' | |
| export default function UnauthorizedPage() { | |
| return ( | |
| <main> | |
| <h1>401 - Unauthorized</h1> | |
| <p>Please log in to access this page.</p> | |
| <Login /> | |
| </main> | |
| ) | |
| } | |
| ``` | |
| ```jsx filename="app/unauthorized.js" switcher | |
| import Login from '@/app/components/Login' | |
| export default function UnauthorizedPage() { | |
| return ( | |
| <main> | |
| <h1>401 - Unauthorized</h1> | |
| <p>Please log in to access this page.</p> | |
| <Login /> | |
| </main> | |
| ) | |
| } | |
| ``` | |
| ## Version History | |
| | Version | Changes | | |
| | --------- | ----------------------------- | | |
| | `v15.1.0` | `unauthorized.js` introduced. | | |