Microsoft Entra ID: How to get OID or other claims? (sub not reliable for user id) #12573
Unanswered
benhovinga
asked this question in
Help
Replies: 1 comment
-
I figured out how to get the New code/** auth.ts */
import NextAuth from 'next-auth';
import MicrosoftEntraID from 'next-auth/providers/microsoft-entra-id';
export 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: `https://login.microsoftonline.com/${process.env.AUTH_MICROSOFT_ENTRA_ID_TENANT || 'common'}/v2.0`,
authorization: {
params: {
scope: 'openid profile email User.Read',
prompt: 'select_account'
}
},
async profile(profile) {
return {
id: profile.sub, // <-- Both id and sub should be the same
sub: profile.sub, // <-- Both id and sub should be the same
oid: profile.oid,
email: profile.email,
displayName: profile.name,
firstName: profile.given_name,
lastName: profile.family_name,
username: profile.preferred_username
};
}
})
],
callbacks: {
async jwt({ token, user }) {
if (user) token.user = user;
return token;
},
async session({ session, token }) {
session.user = token.user;
return session;
}
}
}); Results{
"user": {
"id": "6d9ed1f7-d8ef-400f-b64d-24b603687bad", // <-- Both id and sub should be the same
"sub": "NFepgEpkWsxHWO9q_QPfX61XV01dQeJnK2jGnJHPyqA", // <-- Both id and sub should be the same
"oid": "<hidden>",
"email": "testuser@<hidden>",
"displayName": "Test User",
"firstName": "TestFirstName",
"lastName": "TestLastName",
"username": "testuser@<hidden>"
},
"expires": "2025-02-28T19:04:39.522Z"
} Can someone explain to me why the |
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
-
With next-auth v5, how do I get the
oid
,roles
, or other claims when authenticating? Theuser.id
provided fromsub
is changing every time the user logs in and I can't use this as a reliable user id. I'd like to useoid
instead.I have new nextjs v15 app and everything is left as default. I added
token
to the session for debugging. On the homepage I have<pre>{JSON.stringify(session, null, 2)}</pre>
to dump the entire session to the screen. Each time I log in I can seeuser.id
andtoken.sub
are changing. I also don't see any other claims on the token likeoid
orroles
but think I should be get them because I set the scopes to'openid profile email User.Read'
.I have also tried adding
account
andprofile
totoken
but they don't return anything. I expect to see at least theid_token
oraccess_token
inaccount
.Login results
First login
Second Login
My Code
Beta Was this translation helpful? Give feedback.
All reactions