By using Django and allauth, how can we stop authentication of a user with same email address for two social accounts?

Viewed 106

I am new to Django and was trying to implement the below-mentioned scenario which is as such:

I have a google account as well as a facebook account with the same email address by using django-allauth. So if I am already logged in using google/facebook account and try to login from facebook/google i.e. the other social account in the same session, I should show that the user has already been logged in with this email ID. My approach is to set email and provider id in the session request and manipulate it in the pre_social_login signal in my social adapter.

So far I have defined my middleware as such:

middleware.py

from django.contrib import messages
class SimpleMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.
    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.
        if request.path == '/accounts/login/' and request.method == 'GET' and not request.user.is_authenticated:
            if 'email' not in request.session:
                request.session['email'] = ''
                request.session['provider']=''
                request.session['is_active']='False'         
                
        response = self.get_response(request)
        return response

my_adapter.py

from allauth import account
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter

    
    class MySocialAccountAdapter(DefaultSocialAccountAdapter):
    
        def pre_social_login(self, request, sociallogin):
            if request.session['email']=='' and request.session['provider']=='':
                request.session['email']=sociallogin.account.extra_data['email']
                request.request.session['provider']=sociallogin.account.provider
                request.session['is_active']=False
                response = self.get_response(request)
            
            
            elif request.session['email']==sociallogin.account.extra_data['email'] and request.session['provider']!=sociallogin.account.provider:
                 request.session['is_active']=True
                 response = self.get_response(request)      
     
            
            else:
                request.session['is_active']=True
                response = self.get_response(request)
            
            return response

But after the accounts/login/callback is hit, the following error is thrown:

AttributeError at /accounts/google/login/callback/ 'WSGIRequest' object has no attribute 'request' Request Method: GET Request URL: https://a2d3-110-36-226-234.ngrok.io/accounts/google/login/callback/?state=22U9GJsvDe3e&code=4%2F0AX4XfWg-xpibKpAXe-Bl0Tpj8Un_PLLNZ-seIwyI0QGgOPbJdHZW1Wt87WZoRMztn5Jikg&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=none Django Version: 4.0 Exception Type: AttributeError Exception Value:
'WSGIRequest' object has no attribute 'request' Exception Location: C:\Users\user\Desktop\project\social\login_app\my_adapter.py, line 13, in pre_social_login Python Executable: C:\Users\user\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe Python Version: 3.9.9

ANY SUGGESTIONS, PLEASE?

0 Answers
Related