You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is my login page code. I want to use get_token_by_login to handle the login process. If the login is successful, it should return a token. I want this token to be stored in the user's cookies because I will use this token later for user authentication. The code is as follows:
fromfastapiimportAPIRouter, Responsefrombin.authenticateimportget_token_by_loginfrombinimportui, loginDBrouter=APIRouter()
@ui.page("/user/login")defpage_login(response: Response):
deftry_login():
try:
# This function return a str such as :eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50IjoiYmFvemkiLCJleHAiOjE3MzkxMjM4MDF9.oauwpf1FyeM1zEQw1dFc5wmeG3qnnX5uDZ6RpWt1K7ctoken=get_token_by_login(account.value, password.value)
response.set_cookie("token", token)
ui.navigate.to("/")
exceptloginDB.FailedLoginErrorase:
ui.notify(f"Failed login:{e}")
withui.card().classes('absolute-center'):
account=ui.input('账户ID')
password=ui.input("密码", password=True, password_toggle_button=True).on('keydown.enter', try_login)
ui.button("登陆", on_click=try_login)
Here is the authentication code. Every time a user accesses a page, the following middleware is triggered first. I want to extract the token from the user's requests cookies, then use the check_token` function to verify authentication. If the authentication is successful, the user should be redirected to the desired page; otherwise, they should be redirected to the login page.
However, this is not working yet because I encountered an issue I can't resolve. The problem is that the request's cookies do not contain the token—only the session.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Question
Here is my login page code. I want to use
get_token_by_login
to handle the login process. If the login is successful, it should return atoken
. I want this token to be stored in theuser's cookies
because I will use this token later for user authentication. The code is as follows:Here is the authentication code. Every time a user accesses a page, the following
middleware
is triggered first. I want to extract the token from the user's requests cookies, then use the
check_token` function to verify authentication. If the authentication is successful, the user should be redirected to the desired page; otherwise, they should be redirected to the login page.However, this is not working yet because I encountered an issue I can't resolve. The problem is that the request's cookies do not contain the token—only the session.
The code is as follows:
The login function has already set the
token cookie
, but in reality, the user does not receive it—only thesession cookie
is present.So, I tried changing the key name from
token
tosession
in an attempt to overwrite the existing session, but it had no effect.I also noticed that I am using
storage_secret
, but even when I set it to None, it still doesn't work.The code is as follows:
Desired solution:
I want to be able to correctly set any number of cookies as I need.
THANKS!!!
Beta Was this translation helpful? Give feedback.
All reactions