·

Keep user logged in using Django and Vue 3

Published at 2024-05-02 18:25:10Viewed 434 times
Professional article
Please reprint with source link

The request.session object will automatically generate a cookie with the default name sessionid, which stores the session's session key value. When the session expires, the cookie will automatically removed. Only when the cookie is cleared can the logged in user log in again.


Set the expiration time of the session through the backend, and when the time is up, you can see through the browser that the cookie will automatically disappear. Therefore, it is only necessary to set that the token value in the user's local storage disappears along with the cookie, and the duration of the user's login status can be controlled by setting the expiration time of the session, such as a seven day no login period.


We may think that login status can be controlled based on cookies, but in reality, it is not easy to manipulate cookie values in Vue. In JavaScript, we can control cookies through instructions such as document. getElementId(). But in Vue, using the document instruction is likely to result in an empty return value. Therefore, controlling login status cannot be achieved through direct control of cookies.


Therefore, we need to write an additional interface in the backend to verify the validity of the token and control the user's login status.


Note: Cookies only affect whether users can log in again, and in Vue, it is inconvenient to use cookies to determine user login. Therefore, only tokens can be used to implement this. However, tokens and cookies are independent in the front-end, so if the cookie expires and the token is not cleared, it will result in continuous login. If the token is cleared and the cookie is not expired, it will cause the user to be unable to log in after the login status ends.

0 人喜欢

Comments

There is no comment, let's add the first one.

弦圈热门内容

Grothendick经典同调代数文章:Some aspects of homological algebra

这是Grothendick著名的关于同调代数的文章Tôhoku paper的英文翻译版,原文是法语版,标题为Sur quelques points d'algèbre homologique。英文翻译为:Some aspects of homological algebra。该文章概述了很多同调代数的重要概念,其中基本都跟代数几何有联系,并且里面不少概念其实是Grothendick本人提出来的,如abelian categories。可以说这篇文章是同调代数的经典文章,在数学圈内也时常有人推荐看这篇文章,毕竟这可是祖师爷亲自从同调代数的基础概念一步步讲起,这对学同调代数或者代数几何的人都有很大裨益。我收藏这篇文章的时候都2021年了,现在拿出来推荐给大家!之后我还会把法语原版也发出来。

Get connected with us on social networks! Twitter

©2024 Guangzhou Sinephony Technology Co., Ltd All Rights Reserved