Add allauth_oauth app to sync user fields on OAuth login
All checks were successful
Docker / build (release) Successful in 8s
All checks were successful
Docker / build (release) Successful in 8s
This commit is contained in:
parent
686cf611b6
commit
893c56874f
2 changed files with 41 additions and 0 deletions
12
allauth_oauth/apps.py
Normal file
12
allauth_oauth/apps.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# This file is part of photo21
|
||||
# Copyright (C) 2022 Amicale des élèves de l'ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AllauthOAuthConfig(AppConfig):
|
||||
name = "allauth_oauth"
|
||||
|
||||
def ready(self):
|
||||
import allauth_oauth.signals # noqa: F401
|
||||
29
allauth_oauth/signals.py
Normal file
29
allauth_oauth/signals.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# This file is part of photo21
|
||||
# Copyright (C) 2022 Amicale des élèves de l'ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from allauth.socialaccount.signals import pre_social_login
|
||||
from django.dispatch import receiver
|
||||
|
||||
|
||||
@receiver(pre_social_login)
|
||||
def sync_user_fields(sender, request, sociallogin, **kwargs):
|
||||
if not sociallogin.is_existing:
|
||||
return
|
||||
|
||||
user = sociallogin.user
|
||||
data = sociallogin.account.extra_data
|
||||
changed = False
|
||||
|
||||
email = data.get("email")
|
||||
if email and user.email != email:
|
||||
user.email = email
|
||||
changed = True
|
||||
|
||||
username = data.get("username")
|
||||
if username and user.username != username:
|
||||
user.username = username
|
||||
changed = True
|
||||
|
||||
if changed:
|
||||
user.save()
|
||||
Loading…
Add table
Add a link
Reference in a new issue