How do I display Job Title and other user properties in my Next Auth app using Azure AD / Entra ID. #11201
Unanswered
charan-vendra
asked this question in
Help
Replies: 1 comment
-
You can fetch the information you need from the microsoft graph API and add those to the JWT auth.config.ts callbacks: {
async jwt({
token,
account
}) {
if (account?.access_token) {
try {
//Extract the roles from the id_token and add them to the token
if (account?.id_token) {
const [_header, payload, _sig] = account.id_token.split(".");
const idToken = JSON.parse(
Buffer.from(payload, "base64").toString("utf8"),
);
token.roles = [...idToken.roles];
}
const userDetails = await getUserDetails(account.access_token);
token.userDetails = {
...userDetails,
roles: token.roles,
};
} catch (error) {
console.error(
"Failed to fetch user details from Microsoft Graph API",
error,
);
}
}
return token;
},
async session({
session,
token
}) {
if (token.userDetails) {
session.user = {
...session.user,
...token.userDetails
};
}
return session;
},
} msGraphApi.ts "use server";
import { Client } from "@microsoft/microsoft-graph-client";
export async function getUserDetails(accessToken: string) {
// https://learn.microsoft.com/en-us/graph/sdks/create-client?tabs=typescript
const client = Client.init({
authProvider: (done) => done(null, accessToken),
});
const userDetails = await client.api("/me").get();
return userDetails;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How do I display Job Title and other user properties in my Next Auth app using Azure AD / Entra ID.
I am using next-auth v5 and Next.js V14 so I have an app directory and no pages directory.
This is my route.tsx (placed in [...nextauth]/):
this is my options.tsx:
and this is my page.tsx:
Please help me. Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions