---
slug: "django-social-auth-で-authalreadyassociated-が出る場合"
title: "What to Do When You Encounter AuthAlreadyAssociated with Django Social Auth"
description: "I'm writing this down because I always get flustered."
url: "https://www.ytyng.com/en/blog/django-social-auth-で-authalreadyassociated-が出る場合"
publish_date: "2017-09-01T02:10:40Z"
created: "2017-09-01T02:10:40Z"
updated: "2026-02-27T10:44:19.723Z"
categories: ["Django", "Python"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/d13535239d294f6986b812158919752c.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# What to Do When You Encounter AuthAlreadyAssociated with Django Social Auth

<p>I'm writing this down because I always get flustered.</p>
<p></p>
<p>python - AuthAlreadyAssociated Exception in Django Social Auth - Stack Overflow<br /><a href="https://stackoverflow.com/questions/13018147/authalreadyassociated-exception-in-django-social-auth">https://stackoverflow.com/questions/13018147/authalreadyassociated-exception-in-django-social-auth</a></p>
<p></p>
<p>Basically, it's as described in this article.</p>
<p></p>
<p>If you're using torico_id_login (TORICO company authentication library):</p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><br /><span style="color: #12069c; font-weight: bold;"># </span><span style="font-weight: bold;">If AuthAlreadyAssociated occurs<br /></span><span style="font-weight: bold;"><br /></span>※ This error won't occur if the user who is already logged in logs out before logging in again.<br /><br /><br />Two approaches<br /><br />A: Force the user to log out (e.g., using Middleware)<br /><br />B: Handle the error in the Auth Pipeline<br /><br /><br /><span style="color: #12069c; font-weight: bold;">## </span><span style="font-weight: bold;">A: Force the user to log out<br /></span><span style="font-weight: bold;"><br /></span>Add the following to MIDDLEWARE_CLASSES in settings:<br /><br /><span style="background-color: #ececec;">    'submodules.torico_id_login.middleware.SocialAuthExceptionMiddleware',<br /></span><br /><br /><span style="color: #12069c; font-weight: bold;">## </span><span style="font-weight: bold;">B: Handle the error in the Auth Pipeline<br /></span><span style="font-weight: bold;"><br /></span>Write the following in settings:<br /><br /><span style="background-color: #ececec;">    SOCIAL_AUTH_PIPELINE = (<br /></span><span style="background-color: #ececec;">        'social.pipeline.social_auth.social_details',<br /></span><span style="background-color: #ececec;">        'social.pipeline.social_auth.social_uid',<br /></span><span style="background-color: #ececec;">        'social.pipeline.social_auth.auth_allowed',<br /></span><span style="background-color: #ececec;">        'submodule.torico_id_login.pipeline.social_user',<br /></span><span style="background-color: #ececec;">        'social.pipeline.user.get_username',<br /></span><span style="background-color: #ececec;">        'social.pipeline.user.create_user',<br /></span><span style="background-color: #ececec;">        'social.pipeline.social_auth.associate_user',<br /></span><span style="background-color: #ececec;">        'social.pipeline.social_auth.load_extra_data',<br /></span><span style="background-color: #ececec;">        'social.pipeline.user.user_details',<br /></span><span style="background-color: #ececec;">    )<br /></span><br />Like this.<br /><br /><br />Now,</pre>
<p style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="background-color: #ececec;">Introducing the content of SocialAuthExceptionMiddleware<br /></span></p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="color: #808080; font-style: italic;"># -*- coding:utf-8 -*-<br /></span><span style="color: #000080; font-weight: bold;">from </span>__future__ <span style="color: #000080; font-weight: bold;">import </span>unicode_literals<br /><br /><span style="color: #000080; font-weight: bold;">from </span>django <span style="color: #000080; font-weight: bold;">import </span>http<br /><span style="color: #000080; font-weight: bold;">from </span>django.conf <span style="color: #000080; font-weight: bold;">import </span>settings<br /><span style="color: #000080; font-weight: bold;">from </span>django.contrib.auth <span style="color: #000080; font-weight: bold;">import </span>logout<br /><span style="color: #000080; font-weight: bold;">from </span>social.apps.django_app <span style="color: #000080; font-weight: bold;">import </span>middleware<br /><span style="color: #000080; font-weight: bold;">from </span>social.exceptions <span style="color: #000080; font-weight: bold;">import </span>AuthAlreadyAssociated<br /><br /><br /><span style="color: #000080; font-weight: bold;">class </span>SocialAuthExceptionMiddleware(middleware.SocialAuthExceptionMiddleware):<br />    <span style="color: #000080; font-weight: bold;">def </span>process_exception(<span style="color: #94558d;">self</span>, request, exception):<br />        <span style="color: #000080; font-weight: bold;">if </span><span style="color: #000080;">isinstance</span>(exception, AuthAlreadyAssociated):<br />            <span style="color: #808080; font-style: italic;"># Log out<br /></span><span style="color: #808080; font-style: italic;">            </span>logout(request)<br />            <span style="color: #808080; font-style: italic;"># Redirect somewhere<br /></span><span style="color: #808080; font-style: italic;">            </span>return_url = <span style="color: #000080;">getattr</span>(<br />                settings, <span style="color: #008080; font-weight: bold;">'SOCIAL_AUTH_AAA_URL'</span>, <span style="color: #000080;">getattr</span>(<br />                    settings, <span style="color: #008080; font-weight: bold;">'SOCIAL_AUTH_LOGIN_REDIRECT_URL'</span>, <span style="color: #008080; font-weight: bold;">'/'</span>))<br />            <span style="color: #000080; font-weight: bold;">return </span>http.HttpResponseRedirect(return_url)<br /><br />        <span style="color: #000080; font-weight: bold;">return </span><span style="color: #000080;">super</span>(SocialAuthExceptionMiddleware, <span style="color: #94558d;">self</span>).process_exception(<br />            exception, AuthAlreadyAssociated)</pre>
