Replies: 10 comments 33 replies
-
Look here, it should be of great interest |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
The front-end returns a token and a refresh token (optional, for features like "Remember Me") during the initial login. When the token expires, the front-end uses the refresh token to request a new token from the API, enabling seamless login. Therefore, the primary purpose of the refresh token is to obtain a new token, which is typically fixed by the backend for a certain duration, such as 7 or 14 days. |
Beta Was this translation helpful? Give feedback.
-
some pseudocode
:param refresh: Whether to generate a refresh token. If all token records of a user are deleted upon logout, multi-login becomes meaningless. |
Beta Was this translation helpful? Give feedback.
-
New token processing logic:
New multi-point login logic: Each user contains a multi-login switch, the default is to turn off multi-login
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Current complete workflow:
Note: If the token expires during this process, refresh_token will have no effect and will cause the authentication to fail and require a new login. |
Beta Was this translation helpful? Give feedback.
-
1、User login, receive token and refresh_token. 2、Perform JWT verification on each API request. If the token has expired, return 401. 3、If the frontend receives a 401 response and the refresh token has not expired, use the refresh token to request a new token. 4、Replace the old token with the new token and continue the previous request. 5、If the refresh token has expired or obtaining a new token fails, abandon the subsequent requests and require the user to log in again. Would this approach be simpler and more logical, in your opinion? |
Beta Was this translation helpful? Give feedback.
-
I overlooked a key factor, refresh_token will always be valid longer than the token validity time, make the following adjustments:
|
Beta Was this translation helpful? Give feedback.
-
Hi, @downdawn We need to make some changes to add more unique storage token, for token we can get it from the request header and we can query it anyway, but for refresh_token we can't do some accurate operations on it like deleting it because there is no place to store it. My idea:
|
Beta Was this translation helpful? Give feedback.
-
Hi, @wu-clan
fastapi/fastapi#7119
fastapi/fastapi#8959
fastapi/fastapi#3580
None of the above discussions have provided a perfect solution.
Here is my idea:
We can use Redis to store tokens with expiration times, support token refresh, and enable features such as single-point login, multi-point login, and distributed deployment.
Tokens can also store additional information, such as role_id, company_id, is_admin, etc. This makes querying more convenient, but there may be security concerns that need to be carefully considered.
Perhaps there are open-source libraries that have already solved these issues.
Beta Was this translation helpful? Give feedback.
All reactions