import flet as ft from pages.auth.register import Register from pages.auth.forgot_password import ForgotPassword from models.users import Users import hashlib class Login: def __init__(self, page: ft.Page, auth) -> None: self.page = page self.auth = auth self.email = ft.TextField(label="E-mail") self.password = ft.TextField( label="Password", password=True, can_reveal_password=True ) self.error = ft.Text(color=ft.Colors.RED) def on_login_btn_click(self, e): email = self.email.value password = self.password.value users = Users() user = users.get_user_by_email(email) if user['password'] == hashlib.md5(password.encode('utf-8')).hexdigest(): self.page.client_storage.set("is_authenticated", True) self.page.client_storage.set('user_id', user['id']) self.page.go('/') else: self.error.value = "Invalid credentials!" self.error.update() def on_register_btn_click(self, e): register = Register(self.page, self.auth, self) self.auth.placeholder.content.clean() self.auth.placeholder.content = register.build() self.auth.placeholder.update() def on_forgot_password_btn_click(self, e): forgot_password = ForgotPassword(self.page, self.auth, self) self.auth.placeholder.content.clean() self.auth.placeholder.content = forgot_password.build() self.auth.placeholder.update() def build(self): return ft.Container( ft.Column( [ self.email, self.password, self.error, ft.Button( "Login", width=150, on_click=self.on_login_btn_click ), ft.Row( [ ft.TextButton( "Register", on_click=self.on_register_btn_click ), ft.TextButton( "Forgot Password", on_click=self.on_forgot_password_btn_click ) ], expand=True, alignment=ft.MainAxisAlignment.SPACE_BETWEEN ), ], spacing=20, horizontal_alignment=ft.CrossAxisAlignment.CENTER ), width=400, )