import flet as ft import requests def login_page(page: ft.Page): page.clean() page.title = "Login Page" page.theme_mode = ft.ThemeMode.LIGHT # Set the theme to light # UI components username_field = ft.TextField(label="Accountname") password_field = ft.TextField(label="Passwort", password=True, can_reveal_password=True) # Create an info text label info_label = ft.Text("", theme_style=ft.TextThemeStyle.TITLE_SMALL) # Function to handle login def login(e): username = username_field.value.strip() # Strip whitespace password = password_field.value.strip() # Validate input if not username or not password: info_label.value = "Bitte Benutzername und Passwort eingeben" info_label.color = "red" page.update() # Update the page to reflect the changes return # Send request over HTTP try: url = 'http://awesom-o.org:8000/login' headers = { 'accept': 'application/json', 'Content-Type': 'application/json' } data = { 'username': username, 'password': password } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: token_data = response.json() page.session.set("username", token_data['username']) page.session.set("access_token", token_data['session_id']) page.go("/") else: error_message = response.json().get("detail") info_label.value = error_message info_label.color = "red" except requests.exceptions.RequestException as e: info_label.value = f"An error occurred: {str(e)}" info_label.color = "red" # Update the page to reflect the changes page.update() # Function to navigate to the register page def go_to_register(e): page.go("/register_student") # Replace with the appropriate route for your register page # Add components to the page page.add( ft.Text("Daltonraum-Buchungssystem der IGS Garbsen", size=30), ft.Text("Login", size=23, weight=ft.FontWeight.BOLD), username_field, password_field, ft.ElevatedButton("Anmelden", on_click=login), info_label, # Add the info label to the page ft.Row([ ft.TextButton("Noch nicht registriert? Registriere dich hier!", on_click=go_to_register), ]) ) # You can run the login page separately or as part of your main app. if __name__ == "__main__": ft.app(target=login_page, view=ft.AppView.WEB_BROWSER)