0

我注意到SAAD未能將現有用戶與social-auth登錄用戶相關聯。這是使用相同的電子郵件地址創建多個用戶。我嘗試通過在settings.py中發表評論來覆蓋此問題,然後我創建了一個用於創建用戶配置文件的視圖。我這增加了管道,AttributeError:'dict'對象在覆蓋「social-app-auth-django」時沒有屬性'user'create_user

SOCIAL_AUTH_PIPELINE = (
... 
'accounts.views.save_profile', 
... 
) 

accounts.views

def save_profile(backend, user, response, *args, **kwargs): 
    username = "" 
    if backend.name == "google-oauth2": 
     email = response["emails"][0]["value"] 
     existing_user = User.objects.get(email=email) 
     username = existing_user.username 
     password = existing_user.password 
     authenticated_user = authenticate(username=username, password=password) 
     login(request,authenticated_user) # I need request to login the user 

這裏的問題是,我需要請求用戶登錄。登錄需要請求和經過身份驗證的用戶作爲參數。假設我添加請求作爲參數,我得到一個AttributeError: 'dict' object has no attribute 'user'

def save_profile(request, backend, user, response, *args, **kwargs): 
    username = "" 
    if backend.name == "google-oauth2": 
     email = response["emails"][0]["value"] 
     existing_user = User.objects.get(email=email) 
     username = existing_user.username 
     password = existing_user.password 
     authenticated_user = authenticate(username=username, password=password) 
     login(request,authenticated_user) 

如何登錄已知用戶名和密碼的用戶?

回溯錯誤:

Traceback (most recent call last): 
    File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response 
response = self.process_exception_by_middleware(e, request) 
    File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 147, in get_response 
response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func 
response = view_func(request, *args, **kwargs) 
    File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view 
return view_func(*args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_django/utils.py", line 50, in wrapper 
return func(request, backend, *args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_django/views.py", line 28, in complete 
redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_core/actions.py", line 41, in do_complete 
user = backend.complete(user=user, *args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_core/backends/base.py", line 39, in complete 
return self.auth_complete(*args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_core/utils.py", line 253, in wrapper 
return func(*args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_core/backends/oauth.py", line 398, in auth_complete 
*args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_core/utils.py", line 253, in wrapper 
return func(*args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_core/backends/oauth.py", line 409, in do_auth 
return self.strategy.authenticate(*args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_django/strategy.py", line 115, in authenticate 
return authenticate(*args, **kwargs) 
    File "/Library/Python/2.7/site-packages/django/contrib/auth/__init__.py", line 74, in authenticate 
user = backend.authenticate(**credentials) 
    File "/Library/Python/2.7/site-packages/social_core/backends/base.py", line 79, in authenticate 
return self.pipeline(pipeline, *args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_core/backends/base.py", line 82, in pipeline 
out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs) 
    File "/Library/Python/2.7/site-packages/social_core/backends/base.py", line 107, in run_pipeline 
result = func(*args, **out) or {} 
    File "/Users/swaggerjeevan07/Desktop/django/mysite/accounts/views.py", line 76, in save_profile 
login(request,authenticated_user) 
    File "/Library/Python/2.7/site-packages/django/contrib/auth/__init__.py", line 97, in login 
user = request.user  # This is the issue 
AttributeError: 'dict' object has no attribute 'user' 

回答

0

我所做的只是添加social_core.pipeline.social_auth.associate_by_email以上create_user管道。現在,它檢查是否有一個帳戶已經與獲得的社交認證電子郵件鏈接。如果沒有,它會創建一個新用戶。

SOCIAL_AUTH_PIPELINE = (
    ... 
    'social_core.pipeline.social_auth.associate_by_email', 
    'social_core.pipeline.user.create_user', 
    'accounts.views.save_profile', 
    'social_core.pipeline.social_auth.associate_user', 
    ... 
) 

def save_profile樣子:

def save_profile(backend, user, response, *args, **kwargs): 
    if backend.name == "google-oauth2": 
     # A dot in username will have url issues 
     username = user.username.replace(".","_") 
     user.username = username 
     user.first_name = user.first_name.capitalize() 
     user.last_name = user.last_name.capitalize()