Guide: Implementing Google and Microsoft OAuth in Next.js with App Router
This guide will walk you through the process of implementing both Google and Microsoft (Azure AD) OAuth in a Next.js application using the App Router.
Prerequisites
- A Next.js project set up with the App Router
- Docker and Docker Compose installed
- Google Cloud Platform account
- Microsoft Azure account
Step 1: Set Up OAuth Credentials
Google OAuth Setup
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Navigate to “APIs & Services” > “Credentials”.
- Click “Create Credentials” and select “OAuth client ID”.
- Choose “Web application” as the application type.
- Add
http://localhost:3000
to the “Authorized JavaScript origins”. - Add
http://localhost:3000/api/auth/callback/google
to the “Authorized redirect URIs”. - Note down the Client ID and Client Secret.
Microsoft Azure AD Setup
- Go to the Azure Portal.
- Navigate to “Azure Active Directory” > “App registrations” > “New registration”.
- Name your application.
- Set the redirect URI to
http://localhost:3000/api/auth/callback/azure-ad
. Important: Ensure you select “Web” as the platform for the redirect URI, not “Mobile & desktop”. - After creation, note down the Application (client) ID and Tenant ID.
- Go to “Certificates & secrets” and create a new client secret. Note down the secret value.
Step 2: Update Environment Variables
Create or update your .env
file in the project root:
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
AZURE_AD_CLIENT_ID=your_azure_client_id
AZURE_AD_CLIENT_SECRET=your_azure_client_secret
AZURE_AD_TENANT_ID=your_azure_tenant_id
NEXTAUTH_SECRET=your_nextauth_secret # generate a secure random string
NEXTAUTH_URL=http://localhost:3000
Step 3: Update Dockerfile.dev
Update your Dockerfile.dev
:
Step 4: Update docker-compose.yml
Update your docker-compose.yml
:
Step 5: Update NextAuth Configuration
Update src/app/api/auth/[...nextauth]/route.ts
:
Step 6: Update Home Page
Update src/app/page.tsx
:
Step 7: Create Dashboard Page
Create src/app/dashboard/page.tsx
:
Step 8: Run the Application
- Ensure all environment variables are set in your
.env
file at the project root. - From the project root, run:
docker compose up --build
- Visit
http://localhost:3000
in your browser.
You should now see options to sign in with both Google and Microsoft. After successful authentication, you’ll be redirected to the dashboard.
Next Steps
- Enhance the dashboard to display more user-specific information.
- Implement database operations for user data.
- Integrate an LLM for generating messages on the dashboard.
- Add proper error handling and loading states.
- Implement sign-out functionality.
Remember to always follow security best practices and keep your credentials and environment variables secure.