Integrating with Next.js
This guide explains how to integrate OAuth2 authentication into a Next.js application using NextAuth.js with AuthAction. It demonstrates how to set up authentication, handle login and logout, and protect routes and API endpoints.
Example Repository: For a complete working example, check out our example repository.
Overview
Section titled “Overview”This application showcases how to configure and handle authentication using AuthAction’s OAuth2 service with Next.js. The setup includes:
- OAuth2 login flow using NextAuth.js with AuthAction as the provider
- Secure JWT-based session management
- Protected routes that require authentication
- Secure API routes with session validation
- Logout handling with AuthAction’s OIDC-compliant logout flow
Prerequisites
Section titled “Prerequisites”Before using this application, ensure you have:
-
Node.js and npm installed: You can download and install them from nodejs.org.
-
AuthAction OAuth2 credentials: You will need the following from your AuthAction setup:
- Tenant Domain
- Client ID
- Client Secret
- Redirect URIs (login and logout)
Installation
Section titled “Installation”-
Clone the repository (if applicable):
Terminal window git clone https://github.com/authaction/authaction-nextjs-example.gitcd authaction-nextjs-example -
Install dependencies:
Terminal window npm install -
Configure your AuthAction credentials:
Create a
.env.local
file in the root of your project with the following environment variables:Terminal window AUTHACTION_TENANT_DOMAIN=your-authaction-tenant-domainAUTHACTION_CLIENT_ID=your-authaction-client-idAUTHACTION_CLIENT_SECRET=your-authaction-client-secretNEXTAUTH_SECRET=your-secure-random-stringNEXTAUTH_URL=http://localhost:3000
-
Start the development server:
Terminal window npm run devThis will start the Next.js application on
http://localhost:3000
. -
Testing Authentication:
- Open your browser and navigate to
http://localhost:3000
. - You will be automatically redirected to the AuthAction login page.
- After successful login, you will be redirected back to the application dashboard.
- The dashboard displays a welcome message and your profile information.
- Click the “Logout” button to be logged out and redirected to the specified logout URL.
- Open your browser and navigate to
Code Explanation
Section titled “Code Explanation”Authentication Configuration (pages/api/auth/[...nextauth].ts
)
Section titled “Authentication Configuration (pages/api/auth/[...nextauth].ts)”- Configures NextAuth.js with AuthAction as the OAuth2 provider
- Uses JWT strategy for session management
- Handles token refresh and session validation
- Maps user information from AuthAction to the Next.js session
Protected Pages (/app/dashboard/page.tsx
)
Section titled “Protected Pages (/app/dashboard/page.tsx)”- Uses
useSession
hook to check authentication status - Displays user information when authenticated
- Shows login prompt when not authenticated
- Includes logout functionality with proper token handling
API Routes (/app/api/profile/route.ts
)
Section titled “API Routes (/app/api/profile/route.ts)”- Demonstrates server-side session validation
- Returns user profile information for authenticated requests
- Implements proper error handling for unauthenticated access
Common Issues
Section titled “Common Issues”-
Redirects not working:
- Ensure that the
NEXTAUTH_URL
matches your application’s base URL - Verify that the redirect URIs in your AuthAction configuration match exactly
- Check that your application is running on the port specified in your environment variables
- Ensure that the
-
Session issues:
- Make sure
NEXTAUTH_SECRET
is set to a secure, random string - Verify that your AuthAction client secret is correct
- Check browser console and server logs for any error messages
- Make sure
-
Network Errors:
- Ensure your application can reach the AuthAction servers
- Verify that there are no firewall rules or network policies blocking the OAuth2 redirects