我注意到SAAD无法将现有用户与社交身份验证登录用户相关联。这是使用相同的电子邮件地址创建多个用户。我试图通过在settings.py,
中注释来覆盖它# 'social_core.pipeline.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'