What to Do When You Encounter AuthAlreadyAssociated with Django Social Auth

Django Python
2017-09-01 11:10 (7 years ago) ytyng

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)
Current rating: 5

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011