Spaces:
Runtime error
Runtime error
| # auth/auth.py | |
| import bcrypt | |
| from auth.db import get_connection | |
| def register_user(username, password): | |
| username = username.strip() | |
| password = password.strip() | |
| if not username or not password: | |
| return False, "Vui lòng nhập tài khoản và mật khẩu" | |
| conn = get_connection() | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT * FROM users WHERE username = ?", (username,)) | |
| if cursor.fetchone(): | |
| conn.close() | |
| return False, "Tài khoản đã tồn tại" | |
| hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt()) | |
| cursor.execute("INSERT INTO users (username, password_hash) VALUES (?, ?)", (username, hashed)) | |
| conn.commit() | |
| conn.close() | |
| return True, "Đăng ký thành công" | |
| def login_user(username, password): | |
| username = username.strip() | |
| password = password.strip() | |
| if not username or not password: | |
| return False, "Vui lòng nhập tài khoản và mật khẩu" | |
| conn = get_connection() | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT password_hash FROM users WHERE username = ?", (username,)) | |
| row = cursor.fetchone() | |
| conn.close() | |
| if row and bcrypt.checkpw(password.encode(), row[0]): | |
| return True, "Đăng nhập thành công" | |
| return False, "Sai tài khoản hoặc mật khẩu" | |
| def save_message(username, message): | |
| if isinstance(message, (list, tuple)): | |
| message = "\n".join(str(m) for m in message) | |
| conn = get_connection() | |
| cursor = conn.cursor() | |
| cursor.execute("INSERT INTO chat_history (username, message) VALUES (?, ?)", (username, message)) | |
| conn.commit() | |
| conn.close() | |
| def clear_history(username): | |
| conn = get_connection() | |
| cursor = conn.cursor() | |
| cursor.execute("DELETE FROM chat_history WHERE username = ?", (username,)) | |
| conn.commit() | |
| conn.close() | |
| # def save_message(username, message, agent_type): | |
| # if isinstance(message, (list, tuple)): | |
| # message = "\n".join(str(m) for m in message) | |
| # conn = get_connection() | |
| # cursor = conn.cursor() | |
| # cursor.execute("INSERT INTO chat_history (username, message, agent_type) VALUES (?, ?, ?)", (username, message, agent_type)) | |
| # conn.commit() | |
| # conn.close() | |
| def load_history(username): | |
| conn = get_connection() | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT message FROM chat_history WHERE username = ? ORDER BY timestamp ASC", (username,)) | |
| rows = cursor.fetchall() | |
| conn.close() | |
| messages = [row[0] for row in rows] | |
| history = [] | |
| for i in range(0, len(messages), 2): | |
| if i + 1 < len(messages): | |
| history.append([messages[i], messages[i + 1]]) | |
| # Convert to ChatbotDataMessage format | |
| from utils.helpers import convert_list_to_chatbot_messages | |
| return convert_list_to_chatbot_messages(history) | |
| # def load_history(username, agent_type): | |
| # conn = get_connection() | |
| # cursor = conn.cursor() | |
| # cursor.execute("SELECT message FROM chat_history WHERE username = ? ORDER BY timestamp ASC", (username,agent_type)) | |
| # rows = cursor.fetchall() | |
| # conn.close() | |
| # messages = [row[0] for row in rows] | |
| # history = [] | |
| # for i in range(0, len(messages), 2): | |
| # if i + 1 < len(messages): | |
| # history.append([messages[i], messages[i + 1]]) | |
| # return history | |
| def logout_user(state): | |
| state.value["user"] = None | |
| state.value["history"] = [] | |
| return "Đã đăng xuất" | |
| # def logout_user(state): | |
| # state.value["user"] = None | |
| # state.value["history"] = [] | |
| # return "Đã đăng xuất" | |