I keep getting this error. I tried both AllAuth and MSAL, but both generate the same error. ("GET /accounts/microsoft/login/callback/?code=M.C534_BAY.2.U.78004....9-c219-c1ea80f3d8c0 HTTP/1.1" 200 1119) I am running on local sever. This is social provider:
'microsoft': {
'APP': {
'client_id': '....-.8da-....',
'secret': '.8Q~........',
'key': '',
},
'SCOPE': [
'openid', # Required for OpenID Connect
'email', # Retrieve user's email
'profile', # Retrieve user's profile information
'User.Read', # Access user's basic info in Microsoft Graph
],
'AUTH_PARAMS': {
'response_type': 'code',
'redirect_uri': 'http://localhost:8000/accounts/microsoft/login/callback/',
'prompt': 'select_account', # Ensures the user selects an account
},
'OAUTH_PKCE_ENABLED': True, # Use PKCE for enhanced security
'TENANT': 'common', # Replace 'common' with your specific tenant ID if needed
'LOGIN_URL': 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
'TOKEN_URL': 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
'GRAPH_URL': 'https://graph.microsoft.com',
},
I changed scopes but no luck!
ACCOUNT_ADAPTER = 'authentication.adapter.CustomAccountAdapter'
SOCIALACCOUNT_ADAPTER = 'authentication.adapter.CustomSocialAccountAdapter'
and my custom adapter, I don't get any log error other than "GET /accounts/microsoft/login/callback/?code=e7-fcd9-c219-c1ea80f3d8c0 HTTP/1.1" 200 1119 on this: "Menu:
Third-Party Login Failure
An error occurred while attempting to login via your third-party account."
When I click on the sign-in option, it will redirect me to another page where I can pick Microsoft, and since I logged in via Microsoft before, it just signs me correctly! So:
- I click on sign in button on my site
- Select Microsoft
- Redirect to Microsoft log in
- Then land to the callback page above with error
- I can here click on sign in and then click on Microsoft and sign in
class CustomSocialAccountAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self, request, sociallogin):
"""
Handle custom logic before social login is processed.
Automatically log in users who log in via Microsoft.
"""
# Check if it's a Microsoft provider
if sociallogin.account.provider == "microsoft":
user = sociallogin.user # The user instance
if user.id: # Check if the user already exists
# Log the user in
login(request, user)
# Redirect to your desired page (e.g., home page)
return redirect("/")
# For other providers or new users, use default behavior
super().pre_social_login(request, sociallogin)
def save_user(self, request, sociallogin, form=None):
"""
Save the user and handle provider-specific field mapping (e.g., Microsoft, Google).
"""
user = super().save_user(request, sociallogin, form)
# Map fields specific to Microsoft
if sociallogin.account.provider == "microsoft":
extra_data = sociallogin.account.extra_data
user.email = extra_data.get("mail", extra_data.get("userPrincipalName", user.email))
user.first_name = extra_data.get("givenName", user.first_name)
user.last_name = extra_data.get("surname", user.last_name)
# Map fields specific to Google
elif sociallogin.account.provider == "google":
extra_data = sociallogin.account.extra_data
user.email = extra_data.get("email", user.email)
user.first_name = extra_data.get("given_name", user.first_name)
user.last_name = extra_data.get("family_name", user.last_name)
user.save()
return user