---
slug: "django-social-auth-で-authalreadyassociated-が出る場合"
title: "Django Social Auth で AuthAlreadyAssociated が出る場合"
description: "いつも焦るので書いておく"
url: "https://www.ytyng.com/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: "ja"
---

# Django Social Auth で AuthAlreadyAssociated が出る場合

<p>いつも焦るので書いておく</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>基本的にはこの記事に有る通り。</p>
<p></p>
<p>torico_id_login (TORICO社認証ライブラリ) を使ってる場合は</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;">AuthAlreadyAssociated が出る場合<br /></span><span style="font-weight: bold;"><br /></span>※ 新たにログインする前に、すでにログインしているユーザーが<br />ログアウトすればこのエラーは出ません。<br /><br /><br />2つのアプローチ<br /><br />A: ユーザーを強制的にログアウト (Middlewareなどで)<br /><br />B: Auth Pipeline でエラーハンドリングする<br /><br /><br /><span style="color: #12069c; font-weight: bold;">## </span><span style="font-weight: bold;">A: ユーザーを強制的にログアウト<br /></span><span style="font-weight: bold;"><br /></span>settings の MIDDLEWARE_CLASSES に<br /><br /><span style="background-color: #ececec;">    'submodules.torico_id_login.middleware.SocialAuthExceptionMiddleware',<br /></span><br />を追加してください。<br /><br /><br /><span style="color: #12069c; font-weight: bold;">## </span><span style="font-weight: bold;">B: Auth Pipeline でエラーハンドリングする<br /></span><span style="font-weight: bold;"><br /></span>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 />こう書いてください。<br /><br /><br />これで、</pre>
<p style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="background-color: #ececec;">SocialAuthExceptionMiddleware <br /></span></p>
<p style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><span style="background-color: #ececec;">の中身を紹介しておくと</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;"># ログアウトして<br /></span><span style="color: #808080; font-style: italic;">            </span>logout(request)<br />            <span style="color: #808080; font-style: italic;"># どこかへ戻す<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>
