Allowing users to log in using their Microsoft Entra credentials.
It is a complete authentication solution. It,
http://localhost:3000/api/auth/callback/microsoft-entra-id
as valueRead more on here
Create a Next.js project by using create-next-app
CLI utility that is provided and recommended by Next.js <i>(lets assume your appname is 'tutorial')</i>
npx create-next-app tutorial
Choose the prompts, ensure you have App Router selected as yes. I choose default options.
✔ Would you like to use TypeScript? … No / Yes ==> Yes
✔ Would you like to use ESLint? … No / Yes ==> Yes
✔ Would you like to use Tailwind CSS? … No / Yes ==> No
✔ Would you like your code inside a `src/` directory? … No / Yes ==> No
✔ Would you like to use App Router? (recommended) … No / Yes ==> Yes
✔ Would you like to use Turbopack for `next dev`? … No / Yes ==> Yes
✔ Would you like to customize the import alias (`@/*` by default)? … No / Yes ==> No
cd tutorial
├── tutorial
│ ├── README.md
│ ├── app
│ │ ├── favicon.ico
│ │ ├── globals.css
│ │ ├── layout.tsx
│ │ ├── page.module.css
│ │ └── page.tsx
│ ├── eslint.config.mjs
│ ├── next-env.d.ts
│ ├── next.config.ts
│ ├── package-lock.json
│ ├── package.json
│ ├── public
│ │ ├── file.svg
│ │ ├── globe.svg
│ │ ├── next.svg
│ │ ├── vercel.svg
│ │ └── window.svg
│ └── tsconfig.json
npm install next-auth@beta
Auth.js encrypts (JWT) tokens, for that it uses cryptographically secure random string which is of at-least 32 characters. It expects that random string to be identified by 'AUTH_SECRET' environment variable. Auth.js provides CLI command to generate it which also adds it to environment file. It does so by,
npx auth secret
This creates secure random string, assigns it to environment variable AUTH_SECRET
and adds it to .env.local
file. The file will be placed in the directory where the command is executed, so ensure that you execute it in the root folder of your application.
To the .env.local
file add other environment variables that are needed.
AUTH_MICROSOFT_ENTRA_ID_ID="<your-client-id>"
AUTH_MICROSOFT_ENTRA_ID_TENANT_ID="https://login.microsoftonline.com/<your-tenant-id>/v2.0"
AUTH_MICROSOFT_ENTRA_ID_SECRET="<your-client-secret>"
You will get the values from Azure portal.
Complete .env.local
file should look like
AUTH_SECRET="<random string generated after npx auth secret>"
AUTH_MICROSOFT_ENTRA_ID_ID="<your-client-id>"
AUTH_MICROSOFT_ENTRA_ID_TENANT_ID="https://login.microsoftonline.com/<your-tenant-id>/v2.0"
AUTH_MICROSOFT_ENTRA_ID_SECRET="<your-client-secret>"
auth.ts
file is the core of the authentication setup. It defines how Auth.js integrates with providers such as Microsoft Entra ID. It manages authentication, session and token handling.
tutorial/auth.ts
.env.local
fileimport NextAuth from "next-auth";
import MicrosoftEntraID from "@auth/core/providers/microsoft-entra-id";
export const { handlers } = NextAuth({
providers: [
MicrosoftEntraID({
clientId: process.env.AUTH_MICROSOFT_ENTRA_ID_ID,
issuer: process.env.AUTH_MICROSOFT_ENTRA_ID_TENANT_ID,
clientSecret: process.env.AUTH_MICROSOFT_ENTRA_ID_SECRET,
authorization: {
params: {
scope: "openid profile email User.Read",
},
},
}),
],
secret: process.env.AUTH_SECRET
});
In Next.js, authentication requests (e.g., /api/auth/signin, /api/auth/session) need to be handled dynamically. The app/api/auth/[...nextauth]/route.ts file links the routing system to the handlers exported from auth.ts.
app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth";
export const { GET, POST } = handlers;
npm run dev
Test Sign in
Visit
http://localhost:3000/api/auth/signin
You should see a sign-in page with Microsoft Entra ID listed as a provider. Login using your valid Microsoft Entra ID credentials.
Test logged in user details
Visit
http://localhost:3000/api/auth/session
You should see session data, including the user’s email, name, and token expiration time.
Test Sign out
Visit
http://localhost:3000/api/auth/signout
You should be logged out and the session should be cleared.
By following these steps, you’ve successfully configured Microsoft Entra ID as a login provider in your Next.js app using Auth.js!