feat: UI login

This commit is contained in:
DEsimas 2024-12-19 20:04:33 +03:00
parent b47278beb6
commit 7bb4ac17df
2 changed files with 147 additions and 10 deletions

View File

@ -1,8 +1,11 @@
import flet as ft import flet as ft
from flet.core.page import RouteChangeEvent
from requests.board_games import get_sections, get_games 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): def update_games(section):
current_section.value = section current_section.value = section
games = get_games(None if section == "Все" else section) games = get_games(None if section == "Все" else section)
@ -19,9 +22,6 @@ def main(page: ft.Page):
) )
page.update() page.update()
print("Rerender!")
page.title = "Весёлые кубики"
sections = [*get_sections(), "Все"] sections = [*get_sections(), "Все"]
sections_buttons = [ft.Button(section, on_click=lambda e, s=section: update_games(s)) for section in 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( page.add(
ft.Row( ft.Row(
[ [
@ -66,17 +86,26 @@ def main(page: ft.Page):
ft.Row([ ft.Row([
ft.Button( ft.Button(
text="Вход", text="Вход",
on_click=lambda e: print("Button 1 clicked"), on_click=lambda e: go_to_login(),
), ),
ft.Button( ft.Button(
text="Регистрация", 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 alignment=ft.MainAxisAlignment.SPACE_BETWEEN, # Adjust alignment if needed
), ),
ft.Column ( ft.Column(
[ft.Row(sections_buttons, alignment=ft.MainAxisAlignment.CENTER)], [ft.Row(sections_buttons, alignment=ft.MainAxisAlignment.CENTER)],
alignment=ft.MainAxisAlignment.START 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) ft.app(main)

14
UI/src/requests/auth.py Normal file
View File

@ -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]