import flet as ft import time import re import hashlib from models.users import Users from pages.auth.forgot_password import ForgotPassword class Register: def __init__(self, page: ft.Page, auth, login) -> None: self.page = page self.auth = auth self.login = login self.email = ft.TextField(label="E-mail") self.password = ft.TextField( label="Password", password=True, can_reveal_password=True ) self.confirm_password = ft.TextField( label="Confirm Password", password=True, can_reveal_password=True ) self.error = ft.Text(color=ft.Colors.RED) def on_login_btn_click(self, e): self.auth.placeholder.content.clean() self.auth.placeholder.content = self.login.build() self.auth.placeholder.update() def on_register_btn_click(self, e): if not self.verify_email(): self.error.value = "Please insert a valid email address!" self.error.update() return if not self.verify_password(): self.error.value = "Please a stronger password!" self.error.update() return if not self.verify_confirm_password(): self.error.value = "Password and confirm password do not match!" self.error.update() return if not self.register_user(): self.error.value = "Email already registred!" self.error.update() return self.error.value = "User registered, you can now login!" self.error.color = ft.Colors.GREEN self.error.update() time.sleep(3) self.auth.placeholder.content.clean() self.auth.placeholder.content = self.login.build() self.auth.placeholder.update() def on_forgot_password_btn_click(self, e): forgot_password = ForgotPassword(self.page, self.auth, self.login) self.auth.placeholder.content.clean() self.auth.placeholder.content = forgot_password.build() self.auth.placeholder.update() def verify_email(self): email = self.email.value if not re.match(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$", email): return False return True def verify_password(self): passwd = self.password.value if len(passwd) >= 8 and re.search(r"\d", passwd) and re.search(r"[A-Z]", passwd): return True return False def verify_confirm_password(self): return True if self.password.value == self.confirm_password.value else False def register_user(self): email = self.email.value password = self.password.value users = Users() if users.get_user_by_email(email) is not None: return False passwd_hash = hashlib.md5(password.encode('utf-8')).hexdigest() users.insert_user(email, passwd_hash) return True def build(self): return ft.Container( ft.Column( [ self.email, self.password, self.confirm_password, self.error, ft.Button( "Register", width=150, on_click=self.on_register_btn_click ), ft.Row( [ ft.TextButton( "Login", on_click=self.on_login_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, )