Home > front end >  How to auto login a user in django if the tab was closed and he wasn't logged out?
How to auto login a user in django if the tab was closed and he wasn't logged out?

Time:02-04

I am working on a Django project. I have a backened(Django) and frontend(HTML, CSS,JS). Everything is working as expected like login, registration, logout, etc. However, one requirement, and I know it's a very common one, is that if the tab is closed after user logs in(he didn't logout), and the user reopens the url of login, he should be taken to the dashboard instead of again asking for login credentials on login screen.

What Have I tried?

I saw some other answers and I tried putting this check in the "login/" url of my page:

request.user.is_authenticated

It always gives false if I place this check on the login page. I put it on login page's Get method because I want that if user was logged in and he tries to go to the login url, he gets redirected to dashboard. But it always gives False.

What I want help with?

It would be very kind if anyone can guide me on how to achieve the above objective.

CodePudding user response:

There's a token that you pass to frontend upon login, and it stored in the cookies. In your case if you get logged out after closing browser tab, that means you're not able to store the token in the browser. As mentioned in a comment, try to use sessions or implement authentication system Simple JWT

CodePudding user response:

The best way is to use cookies. Reference: https://www.tutorialspoint.com/django/django_cookies_handling.htm

  •  Tags:  
  • Related