Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application error: a server side exception has occurred #273

Open
Nihal-1512 opened this issue Dec 5, 2023 · 14 comments · May be fixed by #430
Open

Application error: a server side exception has occurred #273

Nihal-1512 opened this issue Dec 5, 2023 · 14 comments · May be fixed by #430

Comments

@Nihal-1512
Copy link

Nihal-1512 commented Dec 5, 2023

Have been deploying the solution in a customer environment using the Azure CLI and receiving the error seen below. I have was able to deploy the solution and add the client id, client secret and tenant id. When I go to log in to the application, I click on the Azure AD account and I receive the error. I think I have done all the necessary steps so not sure what is missing. This is an urgent priority for the customer so if someone can help, that would be great. Thanks

image

@jorupp
Copy link

jorupp commented Dec 5, 2023

I'd recommend enabling logging on the app service and seeing what the server side message is.

@Nihal-1512
Copy link
Author

This is the error I am receiving below when I inspect the web page:

image

@Nihal-1512
Copy link
Author

Nihal-1512 commented Dec 6, 2023

This is the error I see in log stream below:

image

image

@jorupp
Copy link

jorupp commented Dec 6, 2023

I'm guessing it's getting a empty email address when you log in - check your Azure AD app registration and make sure it's configured to be able to read the user profile (User.Read in API permissions).

Specifically, I suspect user.email is blank/null/undefined here:

return hashValue(user.email);

@Nihal-1512
Copy link
Author

This is what it looks like in my app registration:

image

image

And this is what the code looks like in helper.ts (didn't make any changes):

image

Is there anything I need to change or is there a way to check if user.email is undefined? Thanks for all your help.

@jorupp
Copy link

jorupp commented Dec 6, 2023

That does look like the right settings for the app registration. The only thing I can think is to add some code in key places to check what the values are and deploy that to your app:

Don't directly post the resulting values here (since they may include secure tokens/usernames/etc.), but I'm wondering if there's a tenant-level setting that's hiding the email address or something?

Maybe someone else will have a better idea to try - I've not had issues like that with Azure AD and next-auth, but hope you can figure out where it's going wrong.

@jorupp
Copy link

jorupp commented Dec 6, 2023

One other thing - are you logging in with an account from the same tenant that this app registration is defined in? What is "Supported account types" set to on the application registration (mine are "My organization only").

@checkso
Copy link

checkso commented Dec 7, 2023

does your user has an email address populated in EntraID?

@diallo-bocar
Copy link

diallo-bocar commented Dec 19, 2023

Hello everyone,

I've come across this thread while troubleshooting a similar issue where the email field was not present in the Azure AD profile object. In my case, the preferred_username field contained an email address, which seems to be a common occurrence with Azure AD configurations.

To address this, I made the following changes to the AzureADProvider configuration on azurechat/src/features/auth/auth-api.ts

AzureADProvider({
  clientId: process.env.AZURE_AD_CLIENT_ID!,
  clientSecret: process.env.AZURE_AD_CLIENT_SECRET!,
  tenantId: process.env.AZURE_AD_TENANT_ID!,
  async profile(profile) {
    let newProfile = { ...profile, id: profile.sub };
    // If profile.email is undefined, set it to profile.preferred_username
    if (!newProfile.email) {
      newProfile = { ...newProfile, email: profile.preferred_username };
    }

    // Check if the email (whether original or set to preferred_username) is in the adminEmails list
    newProfile.isAdmin = adminEmails?.includes(
      newProfile.email.toLowerCase()
    );

    return newProfile;
  },
})

This modification ensures that if the email property is not defined in the profile object, it falls back to using the preferred_username.

You might need to check that the preferred_username is in the format of an email address before assigning it to the email field.

This ensures that we only use valid email addresses and avoid potential issues with non-email formatted usernames.

I hope this solution helps anyone else facing the same issue. Please make sure to adjust the code to fit your specific use case and security requirements.

@jwwillman
Copy link

Did this get resolved for you? I am having the same issue. Server side exception after putting in AAD credentials

@checkso
Copy link

checkso commented Dec 29, 2023

Did this get resolved for you? I am having the same issue. Server side exception after putting in AAD credentials

make sure that your user has an email attribute filled.

@preeti-192
Copy link

Also got the same issue, but didn't find a solution.

@diallo-bocar
Copy link

diallo-bocar commented Jan 4, 2024

Did you make sure that your user has an email attribute filled ?

#273 (comment)

You can try to log the user data to see if you have all required fields.

azurechat/src/features/auth/helpers.ts : In userHashedId, add console.log(user);

In this way, you can check wether you have all fields required on your user profile.

@Nihal-1512
Copy link
Author

The error I was facing was due to the customer providing access to me with a different email address than the standard organisations template so the web application worked for the client but it did not work for me. Not sure about the reasoning behind it but it probably has something to do with the app registrations and the email accounts assigned. Thank you all for your help @jorupp @checkso @diallo-bocar. Happy to close this issue unless anyone would like to continue the conversation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants