Next-14-Azure-Auth is a starter template for building secure Next.js v14+ apps with Microsoft Entra ID authentication and a Microsoft Graph API integration. It uses the NextAuth.js library for authentication and Microsoft Graph for fetching user data.
Ensure the following are installed before running the application locally:
-
Register the Application
-
Navigate to the Azure portal.
-
Go to Microsoft Entra ID > App registrations > New registration.
-
Name your application (e.g.,
Next-14-Azure-Auth
). -
Choose "Accounts in this organizational directory only" for Supported account types.
-
Set the Redirect URI to
http://localhost:3000/api/auth/callback/microsoft-entra-id
and click Register.
-
-
Configure the Application
-
On the app's overview page, copy the Application (client) ID and Directory (tenant) ID—you’ll need these in your
.env.local
file. -
Go to Certificates & secrets > Client secrets > New client secret.
-
Add a description (e.g.,
Next-14-Azure-Auth Secret
), set the expiration, and copy the generated secret value to your.env.local
file.
-
-
Set Up API Permissions
-
Navigate to API permissions > Add a permission.
-
Select Microsoft Graph and add these permissions:
User.Read
openid
email
profile
-
Click Grant admin consent to apply these permissions.
-
-
(Optional) Create an Enterprise Application
-
To assign users/groups to the application:
-
Go to Azure Active Directory > Enterprise applications.
-
Find your application by name.
-
Under Users and groups, assign the relevant users/groups.
-
-
Clone the Repository
git clone https://github.com/matt-wigg/next-14-azure-auth.git cd next-14-azure-auth
-
Install Dependencies
npm install
Or, if using yarn:
yarn install
-
Set Up Environment Variables
Create a
.env.local
file in the root of your project and add the following variables: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_TENANT_ID=your_azure_tenant_id
-
Run the Application
Start the development server:
npm run dev
Or, with yarn:
yarn dev
Your application will run at
http://localhost:3000
.