[docs]classAuthService:""" Service class for handling user authentication and registration. Attributes: user_repository (UserRepository): Repository for user data access. """def__init__(self):self.user_repository=UserRepository()
[docs]defpossible_login(self,identifier,password):""" Logs in a user with the given identifier and password. Args: identifier (str): The username or email of the user. password (str): The password of the user. Returns: User: The authenticated user object if login is successful, otherwise None. """# Check first whether the identifier is an email or username.ifre.match(r'[^@]+@[^@]+\.[^@]+',identifier):user=self.user_repository.get_user_by_email(identifier)else:user=self.user_repository.get_user_by_username(identifier)# Check if user exists and password is correct. ifuseranduser.check_password(password):returnuserreturnNone
[docs]defregister(self,username,email,password,is_professional=False):""" Registers a new user in KusiBot. Args: username (str): The username of the new user. email (str): The email of the new user. password (str): The password of the new user. is_professional (bool): Whether the user is a professional user or not. """fromappimportbcrypthashed_password=bcrypt.generate_password_hash(password).decode('utf-8')user=self.user_repository.add_user(username,email,hashed_password,is_professional)returnTrueifuserelseFalse