A
A
Alex Helber2020-11-11 01:36:01
Django
Alex Helber, 2020-11-11 01:36:01

Why does user authentication fail after template rendering?

class LoginView(View):

    def get(self, request, *args, **kwargs):
        print(request.user.is_authenticated)  # False
        user = authenticate(request)  #  Возвращает корректного юзера от кастомного бэкенда
        login(request, user)
        print(request.user.is_authenticated)  # True
        return HttpResponseRedirect('/')

        # {{user.is_authenticated}} = False


Tried via HttpResponse, HttpResponseRedirect and render. Through render, it shows an authorized user, but when going to any page

, the Custom backend logs out again (returns all the rules, but just in case):

class TgLogin_Backend(BaseBackend):

    def authenticate(self, request, **kwargs):
        userdata = dict(request.GET)
        # Check the username/password and return a user.
        tid = int(request.GET['id'])
        del userdata['id'], userdata['auth_date'], userdata['hash']
        for f in userdata:
            userdata[f] = userdata[f][0]
       
        tg_auth_ok = verify_telegram_authentication(
            bot_token=bot_token, request_data=request.GET
        )
       
        if tg_auth_ok:
            user, created = User.objects.get_or_create(tid=tid, **userdata)
            return user

    def get_user(self, user_id):
        try:
            return User.objects.get(tid=user_id)
        except User.DoesNotExist:
            return None

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question