Microsoft Entra ID
Microsoft has renamed Azure AD to Microsoft Entra ID, more information about the new name can be found here.
Resources
Setup
Callback URL
https://example.com/api/auth/callback/microsoft-entra-id
Environment Variables
AUTH_MICROSOFT_ENTRA_ID_ID
AUTH_MICROSOFT_ENTRA_ID_SECRET
AUTH_MICROSOFT_ENTRA_ID_ISSUER
Register Application
-
Log in to the Microsoft Entra admin center.
-
In the left sidebar, navigate to Identity —> Applications —> App Registrations.
-
Click on New registration.
-
Give your application a name. This name will be displayed to the user when they log in.
-
Select the account types you want to allow to log in. The
AUTH_MICROSOFT_ENTRA_ID_ISSUER
variable will be based on the selection you make here.-
Single tenant only - Only allow users from your organization.
https://login.microsoftonline.com/<Directory (tenant) ID>/v2.0
-
Miltitenant - Allow users from any organization.
https://login.microsoftonline.com/organizations/v2.0
-
Miltitenant + Personal - Allow any Microsoft account (work, school, personal).
https://login.microsoftonline.com/common/v2.0
-
Personal Only - Only allow personal Microsoft accounts.
https://login.microsoftonline.com/consumers/v2.0
-
-
Set the Redirect URI platform to
web
and the Callback URI for your application. When developing you will set this to your local host environment (examplehttp://localhost:3000/api/auth/callback/microsoft-entra-id
). -
From the application overview page copy the Application (client) ID and paste it in the
AUTH_MICROSOFT_ENTRA_ID_ID
variable. -
Navigate to Certificates & secrets and create a new client secret.
-
Copy the secret value (this will be hidden when you leave this page) and paste it in the
AUTH_MICROSOFT_ENTRA_ID_SECRET
variable.
Configuration
import NextAuth from "next-auth"
import MicrosoftEntraID from "next-auth/providers/microsoft-entra-id"
const { handlers, auth, signIn, signOut } = NextAuth({
providers: [
MicrosoftEntraID({
clientId: process.env.AUTH_MICROSOFT_ENTRA_ID_ID,
clientSecret: process.env.AUTH_MICROSOFT_ENTRA_ID_SECRET,
issuer: process.env.AUTH_MICROSOFT_ENTRA_ID_ISSUER,
}),
],
})
AUTH_MICROSOFT_ENTRA_ID_ID="<Application (client) ID>"
AUTH_MICROSOFT_ENTRA_ID_SECRET="<Client secret value>"
AUTH_MICROSOFT_ENTRA_ID_ISSUER="https://login.microsoftonline.com/<Directory (tenant) ID>/v2.0"
Notes
-
If the issuer paramater is not set it will default to
https://login.microsoftonline.com/common/v2.0
. -
Microsoft Entra returns the profile picture in an ArrayBuffer, instead of just a URL to the image, so our provider converts it to a base64 encoded image string and returns that instead. See Microsoft Graph profilePhoto. The default image size is 48x48 to avoid running out of space in case the session is saved as a JWT.