Unable to link azure-ad
account to user due to email missing from profile.
#3264
Replies: 4 comments 17 replies
-
Which version are you on? Could you paste in your code/link to a repository? |
Beta Was this translation helpful? Give feedback.
-
I have a related problem. With Microsoft Entra ID (formerly known as AzureAD), for some users the only place you can get the email address is by decoding Of course, a Microsoft user isn't guaranteed to have an email address at all. However, for our customer base that's not an issue. They will always have an email address. The question is getting it. So I look everywhere there can be an email: on the user (where it does sometimes live), on the profile, and in the token in the account. And I'll take it wherever I can find it. My problem is this: despite I can retrieve the email address from This might well work for the majority of providers - and it certainly works fine for Google, which is the other SSO provider we support - but you cannot make this assumption with Microsoft. You need to look in multiple possible places for the email. I cannot find an officially supported way of fixing this, and I do need to fix it, because it causes the rest of our organisation account creation process to play up. The only thing I can think of, which seems a bit grim, is to hook into the I can then read the account row from the database, by looking up based on This feels pretty ropey though, and it feels like something the AzureAD provider should handle. Is there a better way that I can do this than what I'm suggesting above? |
Beta Was this translation helpful? Give feedback.
-
@balazsorban44 I'm going to try upgrading to latest to see if that fixes the problem as we are a little behind. Having looked through the releases I'm not that hopeful, but it has to be step 1 before I do anything more involved. My main worry - as yet unfounded - is that it might push us to do a mass upgrade of packages and, although I'd prefer for us to be more current, this is not a great time for that piece of work as we're trying to onboard a customer. If a simple upgrade doesn't fix it, might you be open to a PR on @auth/core for the AzureAD provider or is this behaviour you'd prefer not to change? |
Beta Was this translation helpful? Give feedback.
-
Actually, don't worry @balazsorban44, this isn't your problem at all. The whole reason this scenario occurs is because we want to allow customer organisations to enforce SSO, which is often a requirement of their compliance regime (e.g., CE+). So SSO enforcement - which means users can only sign up or sign in using SSO - is a concept we've layered on top of NextAuth. This situation only arises because:
I don't get the vibe this is necessarily something NextAuth is intended to support out of the box? As a result, perhaps the fix should sit with us, not within NextAuth. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to get the azure-ad provider working.
Everything seems to work fine except for one thing - the email isn't included in the oauth profile. Instead we get an object like so:
This is linking the account, so I'm only doing this while I'm already logged in. Because of this, the sessionToken and therefore email and account are available in the request context, but not available in the
callbacks.signIn
context where I am handling the account linking / updating.The best way I can think to work around this would be to fork the repo and pass this context through to the signin callback, but this isn't very nice.
Any ideas? Has anyone faced this before?
Beta Was this translation helpful? Give feedback.
All reactions