fix(checkout-returnto): checkout login: return user to checkout after sign in
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
'use client';
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { Suspense, useState } from 'react';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import Link from 'next/link';
|
||||
import { useAuth } from '@/contexts/AuthContext';
|
||||
|
||||
export default function LoginPage() {
|
||||
function LoginForm() {
|
||||
const { login } = useAuth();
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const returnTo = searchParams.get('returnTo') ?? '/';
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
@@ -19,7 +21,7 @@ export default function LoginPage() {
|
||||
const result = await login(email, password);
|
||||
setLoading(false);
|
||||
if (result.ok) {
|
||||
router.push('/');
|
||||
router.push(returnTo);
|
||||
} else {
|
||||
setError(result.error || 'Credenciales inválidas');
|
||||
}
|
||||
@@ -82,3 +84,11 @@ export default function LoginPage() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function LoginPage() {
|
||||
return (
|
||||
<Suspense>
|
||||
<LoginForm />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
'use client';
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { Suspense, useState } from 'react';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import Link from 'next/link';
|
||||
import { useAuth } from '@/contexts/AuthContext';
|
||||
|
||||
export default function RegisterPage() {
|
||||
function RegisterForm() {
|
||||
const { register } = useAuth();
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const returnTo = searchParams.get('returnTo') ?? '/';
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [confirm, setConfirm] = useState('');
|
||||
@@ -25,7 +27,7 @@ export default function RegisterPage() {
|
||||
const result = await register(email, password);
|
||||
setLoading(false);
|
||||
if (result.ok) {
|
||||
setDone(true);
|
||||
router.push(returnTo);
|
||||
} else {
|
||||
setError(result.error || 'Error al crear cuenta');
|
||||
}
|
||||
@@ -120,3 +122,11 @@ export default function RegisterPage() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function RegisterPage() {
|
||||
return (
|
||||
<Suspense>
|
||||
<RegisterForm />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -230,10 +230,10 @@ export default function CheckoutClient() {
|
||||
<h2 className="font-bold text-gray-900 mb-1">¿Ya tienes cuenta?</h2>
|
||||
<p className="text-sm text-gray-600 mb-3">Inicia sesión para una experiencia más rápida.</p>
|
||||
<div className="flex gap-3">
|
||||
<Link href="/auth/login" className="px-4 py-2 bg-[#70ad47] hover:bg-[#5a9040] text-white text-sm font-semibold rounded-lg transition-colors">
|
||||
<Link href="/auth/login?returnTo=%2Fcheckout" className="px-4 py-2 bg-[#70ad47] hover:bg-[#5a9040] text-white text-sm font-semibold rounded-lg transition-colors">
|
||||
Iniciar sesión
|
||||
</Link>
|
||||
<Link href="/auth/register" className="px-4 py-2 border border-gray-300 hover:border-[#70ad47] text-gray-700 text-sm font-semibold rounded-lg transition-colors">
|
||||
<Link href="/auth/register?returnTo=%2Fcheckout" className="px-4 py-2 border border-gray-300 hover:border-[#70ad47] text-gray-700 text-sm font-semibold rounded-lg transition-colors">
|
||||
Crear cuenta
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user