import streamlit as st import json import sys import os import bcrypt from interface.chatbot import connect_to_services # Add the parent directory to the Python path sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) class Login: def __init__(self): if "prototipo" not in st.session_state: prototipo = st.session_state.services["edgedb_client"].query(''' Select Prototipo { id } filter .name = 'rag' ''') st.session_state.prototipo_id = prototipo[0].id if "username" not in st.session_state: st.session_state.username = "" if "user_authorized" not in st.session_state: st.session_state.user_authorized = False if "services" not in st.session_state: oa_client, qdrant_client, edgedb_client = connect_to_services() st.session_state.services = { "oa_client": oa_client, "qdrant_client": qdrant_client, "edgedb_client": edgedb_client } self.username = "" self.password = "" def mount_login_page(self): # Exibição do título e subtítulo st.title("Bem-vindo à ajuda do gov.br") with st.form("login_form"): st.title("Login") username = st.text_input('E-mail:') password = st.text_input('Senha:', type='password') submitted = st.form_submit_button("Entrar") st.text("Não possui uma conta?") create_account = st.form_submit_button("Criar uma conta") if create_account: if not username: st.warning("Digite um email para cadastrar a conta.") if not password: st.warning("Digite uma senha para cadastrar a conta.") if username and password: if "@" not in username: st.warning("Digite um email válido para cadastrar a conta.") else: user = self.validate_credentials(username, password, return_just_user=True) if user: st.warning("Já existe uma conta usando este email") else: # Gerando senha encriptada hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) hashed_password_str = hashed_password.decode('utf-8') st.session_state.services["edgedb_client"].query(''' INSERT User { username := $username, password := $password, prototipo := (SELECT Prototipo FILTER .id = $prototipo_id) } ''', username=username, \ password=hashed_password_str, \ prototipo_id=st.session_state.prototipo_id ) st.success("Conta cadastrada com sucesso.") if submitted: if username and password: checked = self.validate_credentials(username, password) if checked: st.success("Usuário logado!") st.session_state.user_authorized = True st.session_state.username = username st.rerun() else: st.error("Nome de usuário ou senha inválidos.") else: st.warning("Digite o nome do usuário e a senha.") def validate_credentials(self, username, password, return_just_user=False): user = st.session_state.services["edgedb_client"].query(''' SELECT User { username, password } FILTER .username = $username ''', username=username) if return_just_user: if not user: return False return True if not user or not bcrypt.checkpw(password.encode('utf-8'), user[0].password.encode('utf-8')): return False return True