What to Do When You Encounter AuthAlreadyAssociated with Django Social Auth
DjangoPython
2017-09-01 02:10 (9 years ago)

I'm writing this down because I always get flustered.
python - AuthAlreadyAssociated Exception in Django Social Auth - Stack Overflow
https://stackoverflow.com/questions/13018147/authalreadyassociated-exception-in-django-social-auth
Basically, it's as described in this article.
If you're using torico_id_login (TORICO company authentication library):
# If AuthAlreadyAssociated occurs
※ This error won't occur if the user who is already logged in logs out before logging in again.
Two approaches
A: Force the user to log out (e.g., using Middleware)
B: Handle the error in the Auth Pipeline
## A: Force the user to log out
Add the following to MIDDLEWARE_CLASSES in settings:
'submodules.torico_id_login.middleware.SocialAuthExceptionMiddleware',
## B: Handle the error in the Auth Pipeline
Write the following in settings:
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'submodule.torico_id_login.pipeline.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
)
Like this.
Now,
Introducing the content of SocialAuthExceptionMiddleware
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
from django import http
from django.conf import settings
from django.contrib.auth import logout
from social.apps.django_app import middleware
from social.exceptions import AuthAlreadyAssociated
class SocialAuthExceptionMiddleware(middleware.SocialAuthExceptionMiddleware):
def process_exception(self, request, exception):
if isinstance(exception, AuthAlreadyAssociated):
# Log out
logout(request)
# Redirect somewhere
return_url = getattr(
settings, 'SOCIAL_AUTH_AAA_URL', getattr(
settings, 'SOCIAL_AUTH_LOGIN_REDIRECT_URL', '/'))
return http.HttpResponseRedirect(return_url)
return super(SocialAuthExceptionMiddleware, self).process_exception(
exception, AuthAlreadyAssociated)
Please rate this article (No signup or login required)
Current rating: 5.0 (1)
Related posts
Issues with Redirect URLs Being HTTP Instead of HTTPS in Python Social Auth Django or AllAuth
Using Vue.js Webpack Dev Server Proxy for Social Provider Authentication with Django + Python Social Auth
Return Login and Redirect Views by Hardcoding Social Account Provider in django-allauth (When Using AWS Cognito, etc.)
Switching the Logged-in User in Django Using the Django Shell
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.
We look forward to discussing your development needs.