Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question