How to fix the callback issue, error 200

Triple 0 Reputation points
2024-12-01T23:58:09.6366667+00:00

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:

  • sign in
  • sign up

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:

  1. I click on sign in button on my site
  2. Select Microsoft
  3. Redirect to Microsoft log in
  4. Then land to the callback page above with error
  5. 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
Microsoft Security Microsoft Authenticator
{count} votes

1 answer

Sort by: Most helpful
  1. Akhilesh Vallamkonda 15,320 Reputation points Microsoft External Staff Moderator
    2024-12-12T01:52:51.5433333+00:00

    Hi @Triple

    Thank you for reaching us & Sorry for the delay in response.

    The issue is with the redirect URI for Microsoft login. It requires localhost instead of 127.0.0.1. AllAuth or MSAL might have issues handling localhost or HTTP requests.

    To fix this issue.
    Use localhost for the redirect URI in both Microsoft and your code.
    Use HTTPS for your development server or add a redirect URI that uses the http scheme with the 127.0.0.1
    For more information about the URIs for Microsoft Entra applications please go through the article.
    What are the restrictions of redirect URIs for Microsoft Entra applications.

    Hope this helps. Do let us know if you any further queries by responding in the comments section.

    Thanks,

    Akhilesh.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.