How does Django log in multiple users in a browser and how to avoid session coverage?

302 views Asked by At

This is mine views.py file

class UserAPIView(TemplateView, ListCreateAPIView):
    serializer_class = UserSerializer
    queryset = UserProfile.objects.all()
    template_name = 'users/login.html'

    def post(self, request, *args, **kwargs):
        if self.find_password_and_user(request):
            return HttpResponseRedirect(reverse('user:home'))
        else:
            return HttpResponse("False")

    def find_password_and_user(self, request):
        print(request)
        post_username = request.data.get('username')
        post_password = request.data.get('password')
        user = authenticate(username=post_username, password=post_password)
        if user is not None:
            # login(request, user)
            # =====
            backend = None
            session_auth_hash = ''
            if hasattr(user, 'get_session_auth_hash'):
                session_auth_hash = user.get_session_auth_hash()

            if SESSION_KEY in request.session:
                if _get_user_session_key(request) != user.pk or (
                        session_auth_hash and
                        not constant_time_compare(request.session.get(HASH_SESSION_KEY, ''), session_auth_hash)):
                    # To avoid reusing another user's session, create a new, empty
                    # session if the existing session corresponds to a different
                    # authenticated user.
                    request.session.flush()
            else:
                request.session.cycle_key()

            try:
                backend = backend or user.backend
            except AttributeError:
                backends = _get_backends(return_tuples=True)
                if len(backends) == 1:
                    _, backend = backends[0]
                else:
                    raise ValueError(
                        'You have multiple authentication backends configured and '
                        'therefore must provide the `backend` argument or set the '
                        '`backend` attribute on the user.'
                    )
            else:
                if not isinstance(backend, str):
                    raise TypeError('backend must be a dotted import path string (got %r).' % backend)
            request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
            request.session[BACKEND_SESSION_KEY] = backend
            request.session[HASH_SESSION_KEY] = session_auth_hash
            if hasattr(request, 'user'):
                request.user = user
            rotate_token(request)
            user_logged_in.send(sender=user.__class__, request=request, user=user)
            # =====

            user = UserProfile.objects.filter(username=post_username)
            u_password = user.values('password')[0].get('password')
            return check_password(post_password, u_password)
        return False

How can I log in multiple users in one browser at the same time By default, the session of the newly logged in user will override the session of the previous user. (in Django_ In the session table, the session of the previously logged in user is overwritten.) If it is a different browser, it will not be covered. In short, the same browser can only log in to one user at the same time. On the Internet, there is a way to change the session into a list, but there is no clue at all. I can't help it. Thank you very much.

1

There are 1 answers

0
top On

The answer above got two steps. I should have said something wrong. But I haven't found a solution to this problem for nearly a day. This is my first time to use stack overflow, which is also a memorial. ha-ha. Thank you