123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #!/usr/bin/env python3
- """This is just a very simple authentication example.
- Please see the `OAuth2 example at FastAPI <https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/>`_ or
- use the great `Authlib package <https://docs.authlib.org/en/v0.13/client/starlette.html#using-fastapi>`_ to implement a classing real authentication system.
- Here we just demonstrate the NiceGUI integration.
- """
- from fastapi.responses import RedirectResponse
- from nicegui import app, ui
- # in reality users passwords would obviously need to be hashed
- passwords = {'user1': 'pass1', 'user2': 'pass2'}
- @ui.page('/')
- def main_page() -> None:
- if not app.storage.user.get('authenticated', False):
- return RedirectResponse('/login')
- with ui.column().classes('absolute-center items-center'):
- ui.label(f'Hello {app.storage.user["username"]}!').classes('text-2xl')
- ui.button(on_click=lambda: (app.storage.user.clear(), ui.open('/login'))).props('outline round icon=logout')
- @ui.page('/login')
- def login() -> None:
- def try_login() -> None: # local function to avoid passing username and password as arguments
- if passwords.get(username.value) == password.value:
- app.storage.user.update({'username': username.value, 'authenticated': True})
- ui.open('/')
- else:
- ui.notify('Wrong username or password', color='negative')
- 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').on('keydown.enter', try_login).props('type=password')
- ui.button('Log in', on_click=try_login)
- ui.run(storage_secret='THIS_NEEDS_TO_BE_CHANGED')
|