From 9418f0150ddfab25fc4dfe8f7c48131889ae04fb Mon Sep 17 00:00:00 2001 From: patrickwebsdev Date: Fri, 8 Nov 2024 03:19:42 -0300 Subject: [PATCH 1/2] fix: on click sign in button - you redirect to home page --- .env.example | 2 ++ src/app/auth/page.tsx | 9 +++++---- src/components/AsignarSolicitudButton.tsx | 8 +++++--- src/components/auth/Login.tsx | 8 ++++++-- src/components/auth/SignUp.tsx | 7 +++++-- src/components/auth/SocialButton.tsx | 9 +++++++-- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/.env.example b/.env.example index 2c15a743..1ad7baa7 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,5 @@ +NEXT_PUBLIC_BASE_URL=https://ajudadana.es +NEXT_PUBLIC_NODE_ENV=production NEXT_PUBLIC_SUPABASE_URL= NEXT_PUBLIC_SUPABASE_ANON_KEY= SUPABASE_GOOGLE_AUTH_ID= diff --git a/src/app/auth/page.tsx b/src/app/auth/page.tsx index 5397547f..3d7f79bf 100644 --- a/src/app/auth/page.tsx +++ b/src/app/auth/page.tsx @@ -1,17 +1,18 @@ 'use client'; import { useEffect } from 'react'; import Login from '../../components/auth/Login'; -import { useRouter } from 'next/navigation'; +import { useRouter, useSearchParams } from 'next/navigation'; import { authService } from '@/lib/service'; export default function AuthPage() { const router = useRouter(); - + const searchParams = useSearchParams(); + const redirect = searchParams.get('redirect') || '/'; useEffect(() => { async function fetchSession() { const { data: session } = await authService.getSessionUser(); if (session.user) { - router.push('/'); + router.push(redirect); } } fetchSession(); @@ -19,7 +20,7 @@ export default function AuthPage() { return (
- (window.location.href = '/')} /> + router.push(redirect)} redirectUrl={redirect} />
); } diff --git a/src/components/AsignarSolicitudButton.tsx b/src/components/AsignarSolicitudButton.tsx index fabc1921..92d9474e 100644 --- a/src/components/AsignarSolicitudButton.tsx +++ b/src/components/AsignarSolicitudButton.tsx @@ -10,6 +10,7 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { toast } from 'sonner'; import Modal from '@/components/Modal'; import { useModal } from '@/context/ModalProvider'; +import { useRouter } from 'next/navigation'; type AsignarSolicitudButtonProps = { helpRequest: HelpRequestData; @@ -20,6 +21,7 @@ export default function AsignarSolicitudButton({ helpRequest }: AsignarSolicitud const session = useSession(); const userId = session.user?.id; const MODAL_NAME = `Solicitud-${helpRequest.id}`; + const router = useRouter(); const { data: assignments, @@ -89,12 +91,12 @@ export default function AsignarSolicitudButton({ helpRequest }: AsignarSolicitud if (!session || !session.user) return ( - router.push(`/auth?redirect=${encodeURIComponent('/solicitudes/' + helpRequest.id)}`)} className="w-full text-center rounded-xl px-4 py-2 font-semibold text-white sm:w-auto transition-all bg-green-500 hover:bg-green-600" > Iniciar sesion para ayudar - + ); // Verifica el email dentro de additional_info utilizando un casting y encadenamiento opcional diff --git a/src/components/auth/Login.tsx b/src/components/auth/Login.tsx index e1a68e01..e8014e29 100644 --- a/src/components/auth/Login.tsx +++ b/src/components/auth/Login.tsx @@ -7,6 +7,7 @@ import SocialButton from './SocialButton'; type LoginProps = { onSuccessCallback: () => void; + redirectUrl?: string; }; type Status = { @@ -21,7 +22,7 @@ type FormData = { privacyPolicy: string; }; -export default function Login({ onSuccessCallback }: LoginProps) { +export default function Login({ onSuccessCallback, redirectUrl }: LoginProps) { const [isSignUp, setIsSignUp] = useState(false); const [formData, setFormData] = useState({ email: '', @@ -198,7 +199,9 @@ export default function Login({ onSuccessCallback }: LoginProps) {

O prueba con estas opciones:

- Inicia sesión con Google + + Inicia sesión con Google +
)} @@ -207,6 +210,7 @@ export default function Login({ onSuccessCallback }: LoginProps) { onBackButtonClicked={() => { setIsSignUp(false); }} + callback={onSuccessCallback} /> )} diff --git a/src/components/auth/SignUp.tsx b/src/components/auth/SignUp.tsx index c28d2998..2cea1e9d 100644 --- a/src/components/auth/SignUp.tsx +++ b/src/components/auth/SignUp.tsx @@ -24,9 +24,10 @@ type Status = { type SignUpProps = { onBackButtonClicked: () => void; + callback?: () => void; }; -export default function SignUp({ onBackButtonClicked }: SignUpProps) { +export default function SignUp({ onBackButtonClicked, callback = () => {} }: SignUpProps) { const [formData, setFormData] = useState({ nombre: '', email: '', @@ -122,7 +123,9 @@ export default function SignUp({ onBackButtonClicked }: SignUpProps) { // SIGN IN WITH NEW USER CREATED await authService.signIn(formData.email, formData.password); // REDIRECT USER TO HOME PAGE - window.location.href = '/'; + if (typeof callback === 'function') { + callback(); + } }; return ( diff --git a/src/components/auth/SocialButton.tsx b/src/components/auth/SocialButton.tsx index 82ddef35..1f79a4e2 100644 --- a/src/components/auth/SocialButton.tsx +++ b/src/components/auth/SocialButton.tsx @@ -6,17 +6,22 @@ import { ReactNode } from 'react'; type SocialButtonProps = { provider: Provider; children: ReactNode; + redirectUrl?: string; }; -export default function SocialButton({ provider, children }: SocialButtonProps) { +export default function SocialButton({ provider, redirectUrl, children }: SocialButtonProps) { + const baseUrl = + process.env.NEXT_PUBLIC_ENV === 'production' ? process.env.NEXT_PUBLIC_BASE_URL! : 'http://127.0.0.1:3000'; const handleLogin = async (provider: Provider) => { const { data, error } = await supabase.auth.signInWithOAuth({ provider, + options: { + redirectTo: `${baseUrl + redirectUrl}`, + }, }); if (error) { console.error('Error al iniciar sesión con proveedor:', error.message); return; } - if (data?.url) { return redirect(data.url); } From 18478f4cecb7e422738afb69d752e8231dcfd0d7 Mon Sep 17 00:00:00 2001 From: patrickwebsdev Date: Fri, 8 Nov 2024 03:25:01 -0300 Subject: [PATCH 2/2] fix: suspense wrapper --- src/app/auth/page.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/app/auth/page.tsx b/src/app/auth/page.tsx index 3d7f79bf..887b4689 100644 --- a/src/app/auth/page.tsx +++ b/src/app/auth/page.tsx @@ -1,10 +1,18 @@ 'use client'; -import { useEffect } from 'react'; +import { Suspense, useEffect } from 'react'; import Login from '../../components/auth/Login'; import { useRouter, useSearchParams } from 'next/navigation'; import { authService } from '@/lib/service'; -export default function AuthPage() { +export default function AUthPage() { + return ( + + + + ); +} + +function Auth() { const router = useRouter(); const searchParams = useSearchParams(); const redirect = searchParams.get('redirect') || '/';