Skip to content

Commit

Permalink
auth demo: using storage instead of query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
rodja committed Sep 17, 2023
1 parent 3c33070 commit 37ae7c8
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/authentication/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class AuthMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
if not app.storage.user.get('authenticated', False):
if request.url.path in nicegui.globals.page_routes.values() and request.url.path not in unrestricted_page_routes:
return RedirectResponse(f'/login?referrer_path={quote(request.url.path)}')
app.storage.user['referrer_path'] = request.url.path # remember where the user wanted to go
return RedirectResponse('/login')
return await call_next(request)


Expand All @@ -48,11 +49,11 @@ def test_page() -> None:


@ui.page('/login')
def login(referrer_path: str = '') -> Optional[RedirectResponse]:
def login() -> Optional[RedirectResponse]:
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(referrer_path or '/')
ui.open(app.storage.user.get('referrer_path', '/')) # go back to where the user wanted to go
else:
ui.notify('Wrong username or password', color='negative')

Expand Down

0 comments on commit 37ae7c8

Please sign in to comment.