pythondjangoauthenticationgithubpython-social-auth

Authentication using GitHub is not using the primary email


Recently integrated GitHub authentication in my Django website and noticed that Python Social Auth is registering the users using a non-primary email address.

How can that behaviour be modified?


Solution

  • By design, authentication with GitHub uses the email that GitHub returns after authentication. As noted by Alexander,

    The returned email is the user's publicly visible email address (or null if the user has not specified a public email address in their profile).

    Based on the next image, you can see that in my case it'll return me@tiagoperes.eu. Check yours here.

    enter image description here

    So, you can settle that the email used is the public one and not the primary.

    If you still want to use the primary email, you'll need change the pipeline. Start by getting the list email addresses for the authenticated user. You'll get something like this

    [
      {
        "email": "octocat@github.com",
        "verified": true,
        "primary": true,
        "visibility": "public"
      }
    ]
    

    Then, adjust the user creation to use the primary email instead.