From 7bb4ac17dfc4202e6afa95aa99cc7e79c7460c53 Mon Sep 17 00:00:00 2001 From: DEsimas Date: Thu, 19 Dec 2024 20:04:33 +0300 Subject: [PATCH] feat: UI login --- UI/src/main.py | 143 +++++++++++++++++++++++++++++++++++++--- UI/src/requests/auth.py | 14 ++++ 2 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 UI/src/requests/auth.py diff --git a/UI/src/main.py b/UI/src/main.py index 322a51f..1c541f8 100644 --- a/UI/src/main.py +++ b/UI/src/main.py @@ -1,8 +1,11 @@ import flet as ft +from flet.core.page import RouteChangeEvent from requests.board_games import get_sections, get_games +from requests.auth import login as login_req -def main(page: ft.Page): + +def root(page: ft.Page): def update_games(section): current_section.value = section games = get_games(None if section == "Все" else section) @@ -19,9 +22,6 @@ def main(page: ft.Page): ) page.update() - print("Rerender!") - page.title = "Весёлые кубики" - sections = [*get_sections(), "Все"] sections_buttons = [ft.Button(section, on_click=lambda e, s=section: update_games(s)) for section in sections] @@ -54,6 +54,26 @@ def main(page: ft.Page): ]) ) + def go_to_login(): + page.route = "/login" + page.update() + + def go_to_register(): + page.route = "/register" + page.update() + + def go_to_profile(): + page.route = "/profile" + page.update() + + def logout(): + page.client_storage.remove("user_id") + page.clean() + root(page) + page.update() + + user_id = page.client_storage.get("user_id") + page.add( ft.Row( [ @@ -66,17 +86,26 @@ def main(page: ft.Page): ft.Row([ ft.Button( text="Вход", - on_click=lambda e: print("Button 1 clicked"), + on_click=lambda e: go_to_login(), ), ft.Button( text="Регистрация", - on_click=lambda e: print("Button 2 clicked"), + on_click=lambda e: go_to_register(), ), + ]) if user_id is None else ft.Row([ + ft.Button( + "Профиль", + on_click=lambda e: go_to_profile() + ), + ft.Button( + text="Выйти", + on_click=lambda e: logout(), + ) ]) ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN, # Adjust alignment if needed ), - ft.Column ( + ft.Column( [ft.Row(sections_buttons, alignment=ft.MainAxisAlignment.CENTER)], alignment=ft.MainAxisAlignment.START ), @@ -84,7 +113,101 @@ def main(page: ft.Page): ) +def login(page): + def close_banner(e): + page.close(banner) + + action_button_style = ft.ButtonStyle(color=ft.Colors.BLUE) + banner = ft.Banner( + bgcolor=ft.Colors.AMBER_100, + leading=ft.Icon(ft.Icons.WARNING_AMBER_ROUNDED, color=ft.Colors.AMBER, size=40), + content=ft.Text( + value="Ведены неверные данные пользователя", + color=ft.Colors.BLACK, + ), + actions=[ + ft.TextButton(text="Ок", style=action_button_style, on_click=close_banner), + ], + ) + + # Login button handler + def login_clicked(e): + email = email_input.value + password = password_input.value + user_id = login_req(email, password) + if user_id is None: + page.open(banner) + else: + page.client_storage.set("user_id", user_id) + back_clicked(None) + + # Back button handler + def back_clicked(e): + page.go("/") + + # Email input field + email_input = ft.TextField( + label="Email", + hint_text="Enter your email", + width=300, + ) + + # Password input field + password_input = ft.TextField( + label="Password", + hint_text="Enter your password", + password=True, + can_reveal_password=True, + width=300, + ) + + # Login and Back buttons + login_button = ft.ElevatedButton("Войти", on_click=login_clicked) + back_button = ft.TextButton("Назад", on_click=back_clicked) + + page.horizontal_alignment=ft.CrossAxisAlignment.CENTER + # Add components to the page + page.add( + ft.Column( + [ + ft.Text("Вход", size=24, weight=ft.FontWeight.BOLD), + email_input, + password_input, + login_button, + back_button + ], + alignment=ft.MainAxisAlignment.CENTER, + horizontal_alignment=ft.CrossAxisAlignment.CENTER, + ), + ) + + +def register(page): + page.add(ft.Text("owo")) + + +def profile(page): + page.add(ft.Text("uwu")) + + +def main(page: ft.Page): + def route_change(e): + route = e.route + page.clean() + if route == "/login": + login(page) + elif route == "/register": + register(page) + elif route == "/profile": + profile(page) + else: + root(page) + page.update() + + page.title = "Весёлые кубики" + page.on_route_change = route_change + + route_change(RouteChangeEvent(page.route)) + + ft.app(main) - - - diff --git a/UI/src/requests/auth.py b/UI/src/requests/auth.py new file mode 100644 index 0000000..57ca085 --- /dev/null +++ b/UI/src/requests/auth.py @@ -0,0 +1,14 @@ +from requests.connection import get_cursor + + +def login(email, password): + cursor = get_cursor() + + cursor.execute(f"select * from clients join auth using(client_id) where email='{email}' and password='{password}'") + + res = cursor.fetchone() + + if res is None: + return None + else: + return res[0] \ No newline at end of file