microsoft / azurechat

🤖 💼 Azure Chat Solution Accelerator powered by Azure Open AI Service
MIT License
1.15k stars 958 forks source link

Application error: a server side exception has occurred #273

Open Nihal-1512 opened 7 months ago

Nihal-1512 commented 7 months ago

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 commented 7 months ago

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

Nihal-1512 commented 7 months ago

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

image

Nihal-1512 commented 7 months ago

This is the error I see in log stream below:

image

image

jorupp commented 7 months ago

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: https://github.com/microsoft/azurechat/blob/601086b1b3b462965d23f355b33438d91b1eb454/src/features/auth/helpers.ts#L17

Nihal-1512 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 6 months ago

does your user has an email address populated in EntraID?

diallo-bocar commented 6 months ago

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 commented 6 months ago

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

checkso commented 6 months ago

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 commented 6 months ago

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

diallo-bocar commented 6 months ago

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

https://github.com/microsoft/azurechat/issues/273#issuecomment-1862504267

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 commented 5 months ago

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.