pennersr / django-allauth

Integrated set of Django applications addressing authentication, registration, account management as well as 3rd party (social) account authentication.
https://allauth.org
MIT License
9.55k stars 3.04k forks source link

username (added via populate_user) is overwritten by save_user #2991

Closed andylolz closed 1 year ago

andylolz commented 2 years ago

I’m authenticating users via email, with ACCOUNT_USERNAME_REQUIRED set to false. I’d like to set a custom username for social account logins, that includes the social account’s UID.

I’ve created a custom social account adapter. In order to generate the custom username, I think I need access to sociallogin.account.uid, which I think rules out using generate_unique_username or populate_username (since these don’t have access to sociallogin).

I’m instead setting the custom username in populate_user. This works correctly for social account auto-signups.

However, without auto-signup, the username is thrown away by this bit of the default account adapter: https://github.com/pennersr/django-allauth/blob/985930d303d54b49204d468b3771c811e687eb90/allauth/account/adapter.py#L233-L239

Would it work to change this to:

username = data.get("username")
user_email(user, email)
if username:
    user_username(user, username)
if first_name:
    user_field(user, "first_name", first_name)
if last_name:
    user_field(user, "last_name", last_name)

Would you be happy to accept a pull request to this effect? Thanks,

andylolz commented 2 years ago

Apologies for rewording this ticket so many times! Hopefully it’s now a bit clearer what I’m trying to do here.

My current workaround is to create a save_user adapter that calls super(), then overwrites the user’s username with the custom one, and then resaves the user. This works okay, but I wonder if there’s a better way.

derek-adair commented 1 year ago

@andylolz Close this and move this conversation to an actual PR! It looks good to me, but i'm just doing triage.

andylolz commented 1 year ago

Great – thank you! Done.