Next.js Setup
@pylo/auth-nextjs handles user authentication for Next.js: login, logout, session management, and
automatic token refresh.
Install
$
pnpm add @pylo/auth-nextjsEnvironment
.env.local
1
PYLO_APP_ID=your-app-idPYLO_APP_ID scopes the auth cookies per app. Find your app id in Settings > Apps in the admin
panel.
Middleware
Create a proxy.ts in your project root:
proxy.ts
1
2
3
4
5
6
7
8
9
10
11
12
import { createPyloProxy } from "@pylo/auth-nextjs";
export const proxy = createPyloProxy({
publicPaths: ["/auth"],
});
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|.*\..*|$).*)",
"/",
],
}This protects all routes by default and redirects unauthenticated users to /auth/login. Tokens
refresh before they expire.
Custom middleware
For custom redirect logic or route protection, see Login & Logout.
Next steps
- Add Login & Logout pages
- Use Server Utilities for auth checks in Server Components
- Add the Next.js SDK for data access