Next-14-Azure-Auth is a starter template for building secure Next.js v14+ Tailwind CSS apps with Microsoft Entra ID authentication. It uses the Next-Auth.js Library for authentication and the Microsoft Graph Client Library for fetching user data.
Before you begin, ensure you have the following installed and set up:
Follow these steps to configure Microsoft Entra ID for authentication:
Next-14-Azure-Auth
).http://localhost:3000/api/auth/callback/microsoft-entra-id
.env
file as AUTH_MICROSOFT_ENTRA_ID_ID
..env
file as AUTH_MICROSOFT_ENTRA_ID_ISSUER
.Next-14-Azure-Auth Secret
) and set an expiration period..env
file as AUTH_MICROSOFT_ENTRA_ID_SECRET
.User.Read
- Access user details.openid
- OpenID Connect authentication.email
- Access email addresses.profile
- Access basic profile information.offline_access
- Enable refresh tokens.Clone the Repository
git clone https://github.com/matt-wigg/next-14-azure-auth.git
cd next-14-azure-auth
Install Dependencies
npm install
Set Up Environment Variables
Create a (or edit the) .env
file in the root directory:
AUTH_SECRET=your_auth_secret
AUTH_MICROSOFT_ENTRA_ID_ID=your_azure_client_id
AUTH_MICROSOFT_ENTRA_ID_SECRET=your_azure_client_secret
AUTH_MICROSOFT_ENTRA_ID_ISSUER=your_azure_tenant_id
To generate the AUTH_SECRET
, run:
openssl rand -base64 32
Start the Development Server
npm run dev
Your application will be available at http://localhost:3000.
[!IMPORTANT] This application is configured for local development. Ensure your
.env
file is properly set up before proceeding.
Category | File/Directory | Description |
---|---|---|
Authentication | src/app/api/[...nextauth]/route.ts |
NextAuth.js routes. Customize providers, callbacks, or session behavior. |
src/services/msGraph.ts |
Microsoft Graph API helpers for fetching user data. | |
src/services/msEntraId.ts |
Microsoft Entra ID-specific authentication logic. | |
src/config/auth.config.ts |
Authentication configuration (e.g., provider settings). | |
src/middleware.ts |
Middleware for authentication and route protection. | |
Components | src/components/sign-in-form.tsx |
Sign-in form UI. |
src/components/sign-out-form.tsx |
Sign-out button UI. | |
Configuration | src/config/routes.config.ts |
Route access settings for authenticated/unauthenticated pages. |
src/config/auth.config.ts |
Authentication configuration (e.g., provider settings). | |
.env / .env.local |
Environment variables for sensitive credentials. | |
Styling | tailwind.config.ts |
Tailwind CSS configuration. |
Types | src/types/ |
TypeScript definitions for authentication and middleware. |
Please refer to the Next.js deployment documentation for instructions on deploying your application to various platforms.