|
| 1 | +from __future__ import annotations as _annotations |
| 2 | + |
| 3 | +from typing import Annotated |
| 4 | + |
| 5 | +from fastapi import APIRouter, Depends, Header |
| 6 | +from fastui import AnyComponent, FastUI |
| 7 | +from fastui import components as c |
| 8 | +from fastui.events import AuthEvent, GoToEvent, PageEvent |
| 9 | +from fastui.forms import fastui_form |
| 10 | +from pydantic import BaseModel, EmailStr, Field, SecretStr |
| 11 | + |
| 12 | +from . import db |
| 13 | +from .shared import demo_page |
| 14 | + |
| 15 | +router = APIRouter() |
| 16 | + |
| 17 | + |
| 18 | +async def get_user(authorization: Annotated[str, Header()] = '') -> db.User | None: |
| 19 | + try: |
| 20 | + token = authorization.split(' ', 1)[1] |
| 21 | + except IndexError: |
| 22 | + return None |
| 23 | + else: |
| 24 | + return await db.get_user(token) |
| 25 | + |
| 26 | + |
| 27 | +@router.get('/login', response_model=FastUI, response_model_exclude_none=True) |
| 28 | +def auth_login(user: Annotated[str | None, Depends(get_user)]) -> list[AnyComponent]: |
| 29 | + if user is None: |
| 30 | + return demo_page( |
| 31 | + c.Paragraph( |
| 32 | + text=( |
| 33 | + 'This is a very simple demo of authentication, ' |
| 34 | + 'here you can "login" with any email address and password.' |
| 35 | + ) |
| 36 | + ), |
| 37 | + c.Heading(text='Login'), |
| 38 | + c.ModelForm[LoginForm](submit_url='/api/auth/login'), |
| 39 | + title='Authentication', |
| 40 | + ) |
| 41 | + else: |
| 42 | + return [c.FireEvent(event=GoToEvent(url='/auth/profile'))] |
| 43 | + |
| 44 | + |
| 45 | +class LoginForm(BaseModel): |
| 46 | + email: EmailStr = Field(title='Email Address', description='Enter whatever value you like') |
| 47 | + password: SecretStr = Field( |
| 48 | + title='Password', |
| 49 | + description='Enter whatever value you like, password is not checked', |
| 50 | + json_schema_extra={'autocomplete': 'current-password'}, |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@router.post('/login', response_model=FastUI, response_model_exclude_none=True) |
| 55 | +async def login_form_post(form: Annotated[LoginForm, fastui_form(LoginForm)]) -> list[AnyComponent]: |
| 56 | + token = await db.create_user(form.email) |
| 57 | + return [c.FireEvent(event=AuthEvent(token=token, url='/auth/profile'))] |
| 58 | + |
| 59 | + |
| 60 | +@router.get('/profile', response_model=FastUI, response_model_exclude_none=True) |
| 61 | +async def profile(user: Annotated[db.User | None, Depends(get_user)]) -> list[AnyComponent]: |
| 62 | + if user is None: |
| 63 | + return [c.FireEvent(event=GoToEvent(url='/auth/login'))] |
| 64 | + else: |
| 65 | + active_count = await db.count_users() |
| 66 | + return demo_page( |
| 67 | + c.Paragraph(text=f'You are logged in as "{user.email}", {active_count} active users right now.'), |
| 68 | + c.Button(text='Logout', on_click=PageEvent(name='submit-form')), |
| 69 | + c.Form( |
| 70 | + submit_url='/api/auth/logout', |
| 71 | + form_fields=[c.FormFieldInput(name='test', title='', initial='data', html_type='hidden')], |
| 72 | + footer=[], |
| 73 | + submit_trigger=PageEvent(name='submit-form'), |
| 74 | + ), |
| 75 | + title='Authentication', |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +@router.post('/logout', response_model=FastUI, response_model_exclude_none=True) |
| 80 | +async def logout_form_post(user: Annotated[db.User | None, Depends(get_user)]) -> list[AnyComponent]: |
| 81 | + if user is not None: |
| 82 | + await db.delete_user(user) |
| 83 | + return [c.FireEvent(event=AuthEvent(token=False, url='/auth/login'))] |
0 commit comments