Skip to main content
This example demonstrates how to integrate logto-authkit into a Next.js application with both client and server-side authentication.

Complete Example

1

Install Dependencies

Install logto-authkit and required dependencies:
2

Create Root Layout

Set up the auth provider in your root layout:
app/layout.tsx
3

Create Home Page

Build your landing page with authentication:
app/page.tsx
4

Create Protected Dashboard

Build a dashboard with automatic authentication protection:
app/dashboard/page.tsx
5

Create API Route with Server-Side Auth

Add a protected API route using server-side authentication:
app/api/user/route.ts
6

Add Authentication Pages

Create the required callback and sign-in pages:
7

Configure Environment Variables

Create a .env.local file with your Logto configuration:
.env.local

Server-Side Authentication

logto-authkit supports server-side authentication for API routes and middleware:
app/api/protected/route.ts

Features Demonstrated

  • Next.js App Router: Full integration with App Router
  • Client-Side Auth: Protected pages with automatic redirects
  • Server-Side Auth: Secure API routes with JWT verification
  • Environment Variables: Proper configuration management
  • Popup Sign-In: Optional popup-based authentication
  • User Center: Pre-built UI component for user management

Running the Example

Visit http://localhost:3000 to see your Next.js app with authentication.

Next Steps

Protected Routes

Advanced route protection patterns

Server Integration

Learn more about server-side auth