Spaces:
Sleeping
Sleeping
Rename ui.py to auth.py
Browse files
auth.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# auth.py
|
| 2 |
+
from passlib.context import CryptContext
|
| 3 |
+
from jose import JWTError, jwt
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
from database import SessionLocal, User
|
| 6 |
+
|
| 7 |
+
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 8 |
+
SECRET_KEY = "REPLACE_ME_WITH_ENV_JWT_SECRET"
|
| 9 |
+
ALGORITHM = "HS256"
|
| 10 |
+
ACCESS_TOKEN_EXPIRE_MINUTES = 60
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def hash_password(password: str):
|
| 14 |
+
return pwd_context.hash(password)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def verify_password(plain, hashed):
|
| 18 |
+
return pwd_context.verify(plain, hashed)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def create_access_token(data: dict, expires_delta: timedelta | None = None):
|
| 22 |
+
to_encode = data.copy()
|
| 23 |
+
if expires_delta:
|
| 24 |
+
expire = datetime.utcnow() + expires_delta
|
| 25 |
+
else:
|
| 26 |
+
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
| 27 |
+
to_encode.update({"exp": expire})
|
| 28 |
+
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
| 29 |
+
return encoded_jwt
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def get_current_user(token: str):
|
| 33 |
+
try:
|
| 34 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
| 35 |
+
user_id: int = payload.get("sub")
|
| 36 |
+
if user_id is None:
|
| 37 |
+
return None
|
| 38 |
+
except JWTError:
|
| 39 |
+
return None
|
| 40 |
+
db = SessionLocal()
|
| 41 |
+
return db.query(User).filter(User.id == user_id).first()
|
ui.py
DELETED
|
@@ -1,255 +0,0 @@
|
|
| 1 |
-
# ui.py
|
| 2 |
-
def generate_login_page():
|
| 3 |
-
"""Genera el HTML para la página de login."""
|
| 4 |
-
return """
|
| 5 |
-
<!DOCTYPE html>
|
| 6 |
-
<html lang="es-AR">
|
| 7 |
-
<head>
|
| 8 |
-
<meta charset="UTF-8">
|
| 9 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 10 |
-
<title>Samuel - Iniciar Sesión</title>
|
| 11 |
-
<link rel="stylesheet" href="/static/style.css">
|
| 12 |
-
</head>
|
| 13 |
-
<body>
|
| 14 |
-
<div class="auth-container">
|
| 15 |
-
<div class="auth-form">
|
| 16 |
-
<h1>Samuel</h1>
|
| 17 |
-
<h2>Tu Confidente Digital</h2>
|
| 18 |
-
|
| 19 |
-
<form id="login-form">
|
| 20 |
-
<div class="form-group">
|
| 21 |
-
<label for="username">Usuario o Email</label>
|
| 22 |
-
<input type="text" id="username" name="username" required>
|
| 23 |
-
</div>
|
| 24 |
-
|
| 25 |
-
<div class="form-group">
|
| 26 |
-
<label for="password">Contraseña</label>
|
| 27 |
-
<input type="password" id="password" name="password" required>
|
| 28 |
-
</div>
|
| 29 |
-
|
| 30 |
-
<button type="submit" class="btn-primary">Iniciar Sesión</button>
|
| 31 |
-
</form>
|
| 32 |
-
|
| 33 |
-
<div class="auth-links">
|
| 34 |
-
<p>¿No tienes cuenta? <a href="/register">Regístrate</a></p>
|
| 35 |
-
</div>
|
| 36 |
-
</div>
|
| 37 |
-
</div>
|
| 38 |
-
|
| 39 |
-
<script src="/static/auth.js"></script>
|
| 40 |
-
</body>
|
| 41 |
-
</html>
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
def generate_register_page():
|
| 45 |
-
"""Genera el HTML para la página de registro."""
|
| 46 |
-
return """
|
| 47 |
-
<!DOCTYPE html>
|
| 48 |
-
<html lang="es-AR">
|
| 49 |
-
<head>
|
| 50 |
-
<meta charset="UTF-8">
|
| 51 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 52 |
-
<title>Samuel - Registrarse</title>
|
| 53 |
-
<link rel="stylesheet" href="/static/style.css">
|
| 54 |
-
</head>
|
| 55 |
-
<body>
|
| 56 |
-
<div class="auth-container">
|
| 57 |
-
<div class="auth-form">
|
| 58 |
-
<h1>Samuel</h1>
|
| 59 |
-
<h2>Crear Cuenta</h2>
|
| 60 |
-
|
| 61 |
-
<form id="register-form">
|
| 62 |
-
<div class="form-group">
|
| 63 |
-
<label for="username">Usuario</label>
|
| 64 |
-
<input type="text" id="username" name="username" required>
|
| 65 |
-
</div>
|
| 66 |
-
|
| 67 |
-
<div class="form-group">
|
| 68 |
-
<label for="email">Email</label>
|
| 69 |
-
<input type="email" id="email" name="email" required>
|
| 70 |
-
</div>
|
| 71 |
-
|
| 72 |
-
<div class="form-group">
|
| 73 |
-
<label for="password">Contraseña</label>
|
| 74 |
-
<input type="password" id="password" name="password" required>
|
| 75 |
-
</div>
|
| 76 |
-
|
| 77 |
-
<div class="form-group">
|
| 78 |
-
<label for="confirm-password">Confirmar Contraseña</label>
|
| 79 |
-
<input type="password" id="confirm-password" name="confirm-password" required>
|
| 80 |
-
</div>
|
| 81 |
-
|
| 82 |
-
<button type="submit" class="btn-primary">Registrarse</button>
|
| 83 |
-
</form>
|
| 84 |
-
|
| 85 |
-
<div class="auth-links">
|
| 86 |
-
<p>¿Ya tienes cuenta? <a href="/login">Iniciar Sesión</a></p>
|
| 87 |
-
</div>
|
| 88 |
-
</div>
|
| 89 |
-
</div>
|
| 90 |
-
|
| 91 |
-
<script src="/static/auth.js"></script>
|
| 92 |
-
</body>
|
| 93 |
-
</html>
|
| 94 |
-
"""
|
| 95 |
-
|
| 96 |
-
def generate_foundational_questions_page():
|
| 97 |
-
"""Genera el HTML para la página de preguntas fundacionales."""
|
| 98 |
-
return """
|
| 99 |
-
<!DOCTYPE html>
|
| 100 |
-
<html lang="es-AR">
|
| 101 |
-
<head>
|
| 102 |
-
<meta charset="UTF-8">
|
| 103 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 104 |
-
<title>Samuel - Conocimiento Mutuo</title>
|
| 105 |
-
<link rel="stylesheet" href="/static/style.css">
|
| 106 |
-
</head>
|
| 107 |
-
<body>
|
| 108 |
-
<div class="container">
|
| 109 |
-
<header>
|
| 110 |
-
<h1>Samuel</h1>
|
| 111 |
-
<p>Conocimiento Mutuo</p>
|
| 112 |
-
</header>
|
| 113 |
-
|
| 114 |
-
<main>
|
| 115 |
-
<div class="questions-container">
|
| 116 |
-
<h2>Para ser un buen amigo, necesito conocerte mejor</h2>
|
| 117 |
-
<p>Responde estas preguntas con honestidad. No hay respuestas correctas o incorrectas.</p>
|
| 118 |
-
|
| 119 |
-
<form id="foundational-questions-form">
|
| 120 |
-
<div class="question-group">
|
| 121 |
-
<label for="q1">1. ¿Qué es lo que más valoras en la vida?</label>
|
| 122 |
-
<textarea id="q1" name="q1" rows="3" required></textarea>
|
| 123 |
-
</div>
|
| 124 |
-
|
| 125 |
-
<div class="question-group">
|
| 126 |
-
<label for="q2">2. ¿Cuál es tu mayor miedo o inseguridad?</label>
|
| 127 |
-
<textarea id="q2" name="q2" rows="3" required></textarea>
|
| 128 |
-
</div>
|
| 129 |
-
|
| 130 |
-
<div class="question-group">
|
| 131 |
-
<label for="q3">3. ¿Qué te hace sentir realmente vivo?</label>
|
| 132 |
-
<textarea id="q3" name="q3" rows="3" required></textarea>
|
| 133 |
-
</div>
|
| 134 |
-
|
| 135 |
-
<div class="question-group">
|
| 136 |
-
<label for="q4">4. ¿Cuál ha sido el momento más difícil que has enfrentado?</label>
|
| 137 |
-
<textarea id="q4" name="q4" rows="3" required></textarea>
|
| 138 |
-
</div>
|
| 139 |
-
|
| 140 |
-
<div class="question-group">
|
| 141 |
-
<label for="q5">5. ¿Qué te gustaría que la gente recordara de ti?</label>
|
| 142 |
-
<textarea id="q5" name="q5" rows="3" required></textarea>
|
| 143 |
-
</div>
|
| 144 |
-
|
| 145 |
-
<button type="submit" class="btn-primary">Comenzar nuestra amistad</button>
|
| 146 |
-
</form>
|
| 147 |
-
</div>
|
| 148 |
-
</main>
|
| 149 |
-
</div>
|
| 150 |
-
|
| 151 |
-
<script src="/static/foundational.js"></script>
|
| 152 |
-
</body>
|
| 153 |
-
</html>
|
| 154 |
-
"""
|
| 155 |
-
|
| 156 |
-
def generate_chat_page():
|
| 157 |
-
"""Genera el HTML para la página del chat."""
|
| 158 |
-
return """
|
| 159 |
-
<!DOCTYPE html>
|
| 160 |
-
<html lang="es-AR">
|
| 161 |
-
<head>
|
| 162 |
-
<meta charset="UTF-8">
|
| 163 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 164 |
-
<title>Samuel - Chat</title>
|
| 165 |
-
<link rel="stylesheet" href="/static/style.css">
|
| 166 |
-
</head>
|
| 167 |
-
<body>
|
| 168 |
-
<div class="app-container">
|
| 169 |
-
<div class="sidebar">
|
| 170 |
-
<div class="user-info">
|
| 171 |
-
<h2>Samuel</h2>
|
| 172 |
-
<p>Tu amigo porteño</p>
|
| 173 |
-
</div>
|
| 174 |
-
|
| 175 |
-
<nav>
|
| 176 |
-
<ul>
|
| 177 |
-
<li><a href="/chat" class="active">Chat</a></li>
|
| 178 |
-
<li><a href="/memory">Memoria</a></li>
|
| 179 |
-
<li><a href="/settings">Configuración</a></li>
|
| 180 |
-
</ul>
|
| 181 |
-
</nav>
|
| 182 |
-
|
| 183 |
-
<div class="logout">
|
| 184 |
-
<a href="/logout">Cerrar Sesión</a>
|
| 185 |
-
</div>
|
| 186 |
-
</div>
|
| 187 |
-
|
| 188 |
-
<div class="main-content">
|
| 189 |
-
<div class="chat-container">
|
| 190 |
-
<div id="messages" class="messages">
|
| 191 |
-
<!-- Los mensajes se cargarán aquí dinámicamente -->
|
| 192 |
-
</div>
|
| 193 |
-
|
| 194 |
-
<div class="input-container">
|
| 195 |
-
<input type="text" id="message-input" placeholder="Escribí un mensaje..." autocomplete="off">
|
| 196 |
-
<button id="send-button">Enviar</button>
|
| 197 |
-
<button id="voice-button">🎤</button>
|
| 198 |
-
</div>
|
| 199 |
-
</div>
|
| 200 |
-
</div>
|
| 201 |
-
</div>
|
| 202 |
-
|
| 203 |
-
<script src="/static/chat.js"></script>
|
| 204 |
-
</body>
|
| 205 |
-
</html>
|
| 206 |
-
"""
|
| 207 |
-
|
| 208 |
-
def generate_memory_page():
|
| 209 |
-
"""Genera el HTML para la página de memoria."""
|
| 210 |
-
return """
|
| 211 |
-
<!DOCTYPE html>
|
| 212 |
-
<html lang="es-AR">
|
| 213 |
-
<head>
|
| 214 |
-
<meta charset="UTF-8">
|
| 215 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 216 |
-
<title>Samuel - Memoria</title>
|
| 217 |
-
<link rel="stylesheet" href="/static/style.css">
|
| 218 |
-
</head>
|
| 219 |
-
<body>
|
| 220 |
-
<div class="app-container">
|
| 221 |
-
<div class="sidebar">
|
| 222 |
-
<div class="user-info">
|
| 223 |
-
<h2>Samuel</h2>
|
| 224 |
-
<p>Tu amigo porteño</p>
|
| 225 |
-
</div>
|
| 226 |
-
|
| 227 |
-
<nav>
|
| 228 |
-
<ul>
|
| 229 |
-
<li><a href="/chat">Chat</a></li>
|
| 230 |
-
<li><a href="/memory" class="active">Memoria</a></li>
|
| 231 |
-
<li><a href="/settings">Configuración</a></li>
|
| 232 |
-
</ul>
|
| 233 |
-
</nav>
|
| 234 |
-
|
| 235 |
-
<div class="logout">
|
| 236 |
-
<a href="/logout">Cerrar Sesión</a>
|
| 237 |
-
</div>
|
| 238 |
-
</div>
|
| 239 |
-
|
| 240 |
-
<div class="main-content">
|
| 241 |
-
<div class="memory-container">
|
| 242 |
-
<h1>Nuestra Historia</h1>
|
| 243 |
-
<p>Estas son las reflexiones y recuerdos que he compartido contigo.</p>
|
| 244 |
-
|
| 245 |
-
<div id="memory-timeline">
|
| 246 |
-
<!-- Las memorias se cargarán aquí dinámicamente -->
|
| 247 |
-
</div>
|
| 248 |
-
</div>
|
| 249 |
-
</div>
|
| 250 |
-
</div>
|
| 251 |
-
|
| 252 |
-
<script src="/static/memory.js"></script>
|
| 253 |
-
</body>
|
| 254 |
-
</html>
|
| 255 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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