-
I want to integrate FastAPI-Users with NiceGUI, there a problem is how to implement a Login page to click 'log in' button to execute user login auth. FastAPI-Users provide the auth route like FastAPI-Users provide a topic 'Create a user programmatically', follow it I can execute user management functions as well, so is there possibility to do user login programmatically? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Since you didn't provide a minimal example, I don't know how you currently implement fastapi-users. I don't know NiceGUI very well, but the login method I have tried to do. fastapi-users refer to this https://fastapi-users.github.io/fastapi-users/latest/configuration/full-example/#sqlalchemy NiceGUI refer to this https://github.com/zauberzeug/nicegui/blob/main/examples/authentication/main.py @ui.page("/login")
async def login(
user_manager: BaseUserManager = Depends(get_user_manager),
):
async def try_login() ->
credentials = OAuth2PasswordRequestForm(username=username.value, password=password.value)
user = await user_manager.authenticate(credentials)
if user is None or not user.is_active:
ui.notify('Wrong username or password', color='negative')
app.storage.user.update({'username': username.value, 'authenticated': True})
ui.navigate.to(app.storage.user.get('referrer_path', '/'))
if app.storage.user.get('authenticated', False):
return RedirectResponse('/')
with ui.card().classes('absolute-center'):
username = ui.input('Username').on('keydown.enter', try_login)
password = ui.input('Password', password=True, password_toggle_button=True).on('keydown.enter', try_login)
ui.button('Log in', on_click=try_login)
return None I'm not sure there's a solution to your problem. |
Beta Was this translation helpful? Give feedback.
@hgalytoby Thanks for your help again! I found I mistakenly append 'authorization' into headers multiple times.
I summary the final solution below:
Config FastAPI-Users into NiceGUI following the official doc.
We need customize programmatically call functions (outside the dependency injection mechanism):