From cb9030eb8d49ae1e5bb5f1f19eaf92868ef3d65d Mon Sep 17 00:00:00 2001 From: Alexis Date: Mon, 13 Apr 2026 14:04:53 +0200 Subject: [PATCH] Proceso login con google --- frontend/src/pages/Login.jsx | 138 +++++++++++++++------------ frontend/src/services/authService.js | 5 + 2 files changed, 80 insertions(+), 63 deletions(-) diff --git a/frontend/src/pages/Login.jsx b/frontend/src/pages/Login.jsx index e6a48ff..9cd64ec 100644 --- a/frontend/src/pages/Login.jsx +++ b/frontend/src/pages/Login.jsx @@ -1,87 +1,99 @@ -import { useState } from 'react'; -import { useNavigate, Link } from 'react-router-dom'; +import React, { useState, useEffect, useRef } from 'react'; +import { Link, useNavigate, useSearchParams } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; import { authService } from '../services/authService'; export default function Login() { - const [email, setEmail] = useState(''); - const [password, setPassword] = useState(''); - const [error, setError] = useState(''); - const navigate = useNavigate(); - const { login } = useAuth(); + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const [error, setError] = useState(''); + const navigate = useNavigate(); + const { login } = useAuth(); + const [searchParams] = useSearchParams(); + const googleLoginProcessed = useRef(false); - const handleSubmit = async (e) => { - e.preventDefault(); - setError(''); + useEffect(() => { + const token = searchParams.get('token'); + + if (token && !googleLoginProcessed.current) { + googleLoginProcessed.current = true; + const url = new URL(window.location); + url.searchParams.delete('token'); + window.history.replaceState({}, '', url); + + const fetchUserAndLogin = async () => { try { - const data = await authService.login(email, password); - - const userData = { - id: data.user_id, - username: data.username, - email: email - }; - - login(userData, data.token); - navigate('/'); + localStorage.setItem('token', token); + + const realUserData = await authService.getCurrentUser(); + + login({ user: realUserData, token }); + + navigate('/', { replace: true }); } catch (err) { - setError(err.response?.data?.detail || 'Error al iniciar sesión. Revisa tus credenciales.'); + console.error("Error al sincronizar perfil de Google:", err); + setError("Error de sincronización con Google. Inténtalo de nuevo."); + localStorage.removeItem('token'); } - }; + }; + + fetchUserAndLogin(); + } + }, [searchParams, login, navigate]); + + const handleSubmit = async (e) => { + e.preventDefault(); + setError(''); + try { + const userData = await authService.login(email, password); + login(userData); + navigate('/'); + } catch (err) { + setError('Credenciales incorrectas.'); + } + }; + + const handleGoogleLogin = () => { + window.location.href = "http://localhost:8000/api/auth/google/login"; + }; return ( -
-
+
+
+
-

Bienvenido

-

Inicia sesión para guardar tus espectros difusos

+

Deck of Cards

+

Accede a tu panel de control

{error && ( -
- {error} -
+
{error}
)} -
-
- - setEmail(e.target.value)} - placeholder="tu@email.com" - /> + +
+ + setEmail(e.target.value)} className="w-full px-5 py-3 rounded-2xl border border-slate-200 focus:ring-2 focus:ring-blue-500 outline-none transition-all bg-slate-50 focus:bg-white" placeholder="correo@ejemplo.com" />
- -
- - setPassword(e.target.value)} - placeholder="••••••••" - /> +
+ + setPassword(e.target.value)} className="w-full px-5 py-3 rounded-2xl border border-slate-200 focus:ring-2 focus:ring-blue-500 outline-none transition-all bg-slate-50 focus:bg-white" placeholder="••••••••" />
- - + -

- ¿No tienes cuenta? Regístrate aquí -

+
+
+
O
+
+ + + +

¿Nuevo por aquí? Crea una cuenta

); diff --git a/frontend/src/services/authService.js b/frontend/src/services/authService.js index be72b64..f568a1d 100644 --- a/frontend/src/services/authService.js +++ b/frontend/src/services/authService.js @@ -9,5 +9,10 @@ export const authService = { register: async (username, email, password) => { const response = await api.post('/auth/register', { username, email, password }); return response.data; + }, + + getCurrentUser: async () => { + const response = await api.get('/auth/me'); + return response.data; } }; \ No newline at end of file