init commit

This commit is contained in:
2025-08-31 17:55:26 +03:00
commit 876ddec94a
78 changed files with 11999 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
import flet as ft
from pages.register_page import Register
from pages.forgot_password_page import ForgotPassword
from pages.two_factor_page import TwoFactorAuth
import requests
from config import API_BASE_URL
class Login:
def __init__(self, page: ft.Page, auth):
self.page = page
self.auth = auth
self.email = ft.TextField(label="Email")
self.passwd = ft.TextField(label="Password", password=True, can_reveal_password=True)
self.error_message = ft.Text("", color=ft.Colors.RED)
def on_login_click(self, e):
self.error_message.value = ""
self.error_message.update()
self.page.update()
email = self.email.value.strip()
password = self.passwd.value.strip()
if not email or not password:
self.error_message.value = "Please enter both email and password."
self.error_message.update()
return
# self.auth.placeholder.content.clean()
# two_factor = TwoFactorAuth(self.page, email, self, self.auth)
# self.auth.placeholder.content = two_factor.build()
# self.auth.placeholder.update()
try:
response = requests.post(
f"{API_BASE_URL}/auth/login",
json={"email": email, "password": password},
timeout=10
)
if response.status_code == 200:
user_info = requests.get(
f"{API_BASE_URL}/auth/me",
headers={"Authorization": f"Bearer {response.json().get('access_token', '')}"}
)
if user_info.status_code == 200:
logo_filename = user_info.json().get("logo_filename", "")
if logo_filename:
self.page.client_storage.set("custom_logo", logo_filename)
self.auth.placeholder.content.clean()
two_factor = TwoFactorAuth(self.page, email, self, self.auth)
self.auth.placeholder.content = two_factor.build()
self.auth.placeholder.update()
else:
self.error_message.value = response.json().get("error", "Login failed")
self.error_message.update()
except Exception as ex:
self.error_message.value = f"Login error: {str(ex)}"
self.error_message.update()
def on_forgot_btn_click(self, e):
self.auth.placeholder.content.clean()
forgot_passwd = ForgotPassword(self.page, self.auth, self)
self.auth.placeholder.content = forgot_passwd.build()
self.auth.placeholder.update()
def on_register_btn_click(self, e):
print('Go to register')
register = Register(self.page, self.auth, self)
self.auth.placeholder.content.clean()
self.auth.placeholder.content = register.build()
self.auth.placeholder.update()
def build(self):
return ft.Column(
[
ft.Text(
"Login",
size=30,
weight="bold"
),
self.email,
self.passwd,
ft.ElevatedButton(
"Login",
width = 150,
on_click=self.on_login_click
),
self.error_message,
ft.Row(
[
ft.TextButton("Forgot Password?", on_click=self.on_forgot_btn_click),
ft.TextButton("Register", on_click=self.on_register_btn_click)
],
alignment=ft.MainAxisAlignment.SPACE_BETWEEN
)
],
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
alignment=ft.MainAxisAlignment.CENTER,
spacing=20,
width=350
)