create-appncy
Version:
Create projects as goncy would
83 lines (62 loc) • 2.2 kB
Markdown
Instructions on how to setup auth on the app.
You can mostly follow up [this interactive guide](https://auth0.com/docs/quickstart/webapp/nextjs/interactive) to walk you through.
[](https://manage.auth0.com/) and set the Callback URLs, Logout URLs and Allowed Web Origins:
```bash
http://localhost:3000/api/auth/callback
http://localhost:3000
http://localhost:3000
```
Install the Auth0 dependencies for Next.js:
```bash
pnpm add @auth0/nextjs-auth0
```
Add the following variables to your `.env.local` file:
```bash
AUTH0_SECRET="yourownsecret"
AUTH0_BASE_URL='http://localhost:3000'
AUTH0_ISSUER_BASE_URL='<get this from Auth0>'
AUTH0_CLIENT_ID='<get this from Auth0>'
AUTH0_CLIENT_SECRET='<get this from Auth0>'
```
Create a `src/app/api/auth/[auth0]/route.ts` file with the following content:
```ts
import {handleAuth} from "@auth0/nextjs-auth0";
export const GET = handleAuth() as () => Promise<void>;
```
Wrap the root layout with the `UserProvider` component from `@auth0/nextjs-auth0/client`. This will let you access the session information within client components:
```tsx
// app/layout.tsx
import {UserProvider} from "@/auth/provider";
<UserProvider>{children}</UserProvider>
```
Add a link to login and logout in the app to let users handle their session:
```tsx
// app/layout.tsx
export default async function RootLayout({children}) {
const session = await getSession();
return (
...
<UserProvider>
<header>
{session?.user ? (
<Link href="/api/auth/logout">Cerrar sesión</Link>
) : (
<Link href="/api/auth/login">Iniciar sesión</Link>
)}
</header>
{session?.user && <h1>Hi {session.user.name}!</h1>}
</UserProvider>
...
);
}
```