Fix new Frontend 4
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import React, { useState } from 'react';
|
||||
import { Eye, EyeOff, Loader2, Check } from 'lucide-react';
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
import { useAuth } from '../../api/hooks/useAuth';
|
||||
import type { RegisterRequest } from '../../api/types';
|
||||
|
||||
interface RegisterPageProps {
|
||||
onLogin: (user: any, token: string) => void;
|
||||
onNavigateToLogin: () => void;
|
||||
@@ -15,6 +19,8 @@ interface RegisterForm {
|
||||
}
|
||||
|
||||
const RegisterPage: React.FC<RegisterPageProps> = ({ onLogin, onNavigateToLogin }) => {
|
||||
const { register, isLoading, error } = useAuth();
|
||||
|
||||
const [formData, setFormData] = useState<RegisterForm>({
|
||||
fullName: '',
|
||||
email: '',
|
||||
@@ -25,7 +31,6 @@ const RegisterPage: React.FC<RegisterPageProps> = ({ onLogin, onNavigateToLogin
|
||||
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [errors, setErrors] = useState<Partial<RegisterForm>>({});
|
||||
|
||||
const validateForm = (): boolean => {
|
||||
@@ -47,13 +52,9 @@ const RegisterPage: React.FC<RegisterPageProps> = ({ onLogin, onNavigateToLogin
|
||||
newErrors.password = 'La contraseña es obligatoria';
|
||||
} else if (formData.password.length < 8) {
|
||||
newErrors.password = 'La contraseña debe tener al menos 8 caracteres';
|
||||
} else if (!/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/.test(formData.password)) {
|
||||
newErrors.password = 'La contraseña debe incluir mayúsculas, minúsculas y números';
|
||||
}
|
||||
|
||||
if (!formData.confirmPassword) {
|
||||
newErrors.confirmPassword = 'Confirma tu contraseña';
|
||||
} else if (formData.password !== formData.confirmPassword) {
|
||||
if (formData.password !== formData.confirmPassword) {
|
||||
newErrors.confirmPassword = 'Las contraseñas no coinciden';
|
||||
}
|
||||
|
||||
@@ -70,54 +71,30 @@ const RegisterPage: React.FC<RegisterPageProps> = ({ onLogin, onNavigateToLogin
|
||||
|
||||
if (!validateForm()) return;
|
||||
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/v1/auth/register', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
full_name: formData.fullName,
|
||||
email: formData.email,
|
||||
password: formData.password,
|
||||
role: 'admin' // Default role for bakery owners
|
||||
}),
|
||||
});
|
||||
const registerData: RegisterRequest = {
|
||||
email: formData.email,
|
||||
password: formData.password,
|
||||
full_name: formData.fullName,
|
||||
role: 'user' // Default role
|
||||
};
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || 'Error al crear la cuenta');
|
||||
await register(registerData);
|
||||
|
||||
toast.success('¡Registro exitoso! Bienvenido a PanIA');
|
||||
|
||||
// The useAuth hook handles auto-login after registration
|
||||
// Get the user data from localStorage since useAuth auto-logs in
|
||||
const userData = localStorage.getItem('user_data');
|
||||
const token = localStorage.getItem('auth_token');
|
||||
|
||||
if (userData && token) {
|
||||
onLogin(JSON.parse(userData), token);
|
||||
}
|
||||
|
||||
// Auto-login after successful registration
|
||||
const loginResponse = await fetch('/api/v1/auth/login', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: formData.email,
|
||||
password: formData.password,
|
||||
}),
|
||||
});
|
||||
|
||||
const loginData = await loginResponse.json();
|
||||
|
||||
if (!loginResponse.ok) {
|
||||
throw new Error('Cuenta creada, pero error al iniciar sesión');
|
||||
}
|
||||
|
||||
toast.success('¡Cuenta creada exitosamente! Bienvenido a PanIA');
|
||||
onLogin(loginData.user, loginData.access_token);
|
||||
|
||||
} catch (error: any) {
|
||||
|
||||
} catch (error) {
|
||||
console.error('Registration error:', error);
|
||||
toast.error(error.message || 'Error al crear la cuenta');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
toast.error(error instanceof Error ? error.message : 'Error en el registro');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -372,7 +349,7 @@ const RegisterPage: React.FC<RegisterPageProps> = ({ onLogin, onNavigateToLogin
|
||||
<p className="mt-1 text-sm text-red-600">{errors.acceptTerms}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
|
||||
{/* Submit Button */}
|
||||
<div>
|
||||
<button
|
||||
|
||||
Reference in New Issue
Block a user