fromflaskimportBlueprint,render_template,redirect,url_for,flashfromflask_loginimportlogin_user,login_required,logout_user,current_userfromkusibot.app.auth.formsimportRegisterForm,LoginFormfromkusibot.app.auth.utilsimportredirect_to_principal_pagefromkusibot.servicesimport(AuthService,chatbot_service)auth_bp=Blueprint('auth_bp',__name__,template_folder='templates',static_folder='static')auth_service=AuthService()MESSAGES={'invalid_login':'Invalid username or password.','registration_success':'Registration successful! You can now log in.','registration_error':'An error occurred during registration. Please try again.'}
[docs]@auth_bp.route('/login',methods=['GET','POST'])deflogin():""" Login route for KusiBot. Accepts the following methods: - GET: Goes to principal page if authenticated, else, goes to Login Page. - POST: Log in a user to Kusibot given its username and password. Returns: (Response | str): A redirect response or the HTML login form to render. """# If user is already authenticated, redirect to principal pageifcurrent_user.is_authenticated:returnredirect_to_principal_page(current_user.is_professional)# If user is not authenticated, create login form object.form=LoginForm()# If form is submitted, validate the form data.ifform.validate_on_submit():user=auth_service.possible_login(form.identifier.data,form.password.data)ifuser:login_user(user,remember=form.remember.data)returnredirect_to_principal_page(user.is_professional)else:flash(MESSAGES["invalid_login"],"error")# If form is not submitted, render login page.returnrender_template("login.html",form=form)
[docs]@auth_bp.route('/signup',methods=['GET','POST'])defsignup():""" Register route for KusiBot. Accepts the following methods: - GET: Goes to principal page if authenticated, else, goes to Register Page. - POST: Register a new user in KusiBot. Returns: (Response | str): A redirect response or the HTML register form to render. """# If user is already authenticated, redirect to principal pageifcurrent_user.is_authenticated:returnredirect_to_principal_page(current_user.is_professional)# If user is not authenticated, create register form object.form=RegisterForm()# If form is submitted, validate the form data.ifform.validate_on_submit():# Register a USER (not a professional)success_register=auth_service.register(form.username.data,form.email.data,form.password.data)ifsuccess_register:flash(MESSAGES["registration_success"],"success")fromappimportLOGIN_URLreturnredirect(url_for(LOGIN_URL))else:flash(MESSAGES["registration_error"],"error")# If form is not submitted, render signup page.returnrender_template("signup.html",form=form)
[docs]@auth_bp.route("/logout")@login_requireddeflogout():""" Logs out an authenticated user from KusiBot. Accepts the following methods: - GET: Logs out, ends any current conversation and returns to Main Page. Returns: Response: A redirect response to the main page. """chatbot_service.end_conversation(current_user.id)logout_user()fromappimportMAIN_URLreturnredirect(url_for(MAIN_URL))