Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix: on click sign in button - you redirect to home page" #155

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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=
Expand Down
21 changes: 6 additions & 15 deletions src/app/auth/page.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
'use client';
import { Suspense, useEffect } from 'react';
import { useEffect } from 'react';
import Login from '../../components/auth/Login';
import { useRouter, useSearchParams } from 'next/navigation';
import { useRouter } from 'next/navigation';
import { authService } from '@/lib/service';

export default function AUthPage() {
return (
<Suspense>
<Auth />
</Suspense>
);
}

function Auth() {
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(redirect);
router.push('/');
}
}
fetchSession();
});

return (
<section className="mx-6 lg:m-16">
<Login onSuccessCallback={() => router.push(redirect)} redirectUrl={redirect} />
<Login onSuccessCallback={() => (window.location.href = '/')} />
</section>
);
}
8 changes: 3 additions & 5 deletions src/components/AsignarSolicitudButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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;
Expand All @@ -21,7 +20,6 @@ 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,
Expand Down Expand Up @@ -91,12 +89,12 @@ export default function AsignarSolicitudButton({ helpRequest }: AsignarSolicitud

if (!session || !session.user)
return (
<button
onClick={() => router.push(`/auth?redirect=${encodeURIComponent('/solicitudes/' + helpRequest.id)}`)}
<Link
href="/auth"
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
</button>
</Link>
);

// Verifica el email dentro de additional_info utilizando un casting y encadenamiento opcional
Expand Down
8 changes: 2 additions & 6 deletions src/components/auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import SocialButton from './SocialButton';

type LoginProps = {
onSuccessCallback: () => void;
redirectUrl?: string;
};

type Status = {
Expand All @@ -22,7 +21,7 @@ type FormData = {
privacyPolicy: string;
};

export default function Login({ onSuccessCallback, redirectUrl }: LoginProps) {
export default function Login({ onSuccessCallback }: LoginProps) {
const [isSignUp, setIsSignUp] = useState<boolean>(false);
const [formData, setFormData] = useState<FormData>({
email: '',
Expand Down Expand Up @@ -199,9 +198,7 @@ export default function Login({ onSuccessCallback, redirectUrl }: LoginProps) {

<div className="p-2">
<p className="text-center text-gray-700 mb-2">O prueba con estas opciones:</p>
<SocialButton redirectUrl={redirectUrl} provider="google">
Inicia sesión con Google
</SocialButton>
<SocialButton provider="google">Inicia sesión con Google</SocialButton>
</div>
</form>
)}
Expand All @@ -210,7 +207,6 @@ export default function Login({ onSuccessCallback, redirectUrl }: LoginProps) {
onBackButtonClicked={() => {
setIsSignUp(false);
}}
callback={onSuccessCallback}
/>
)}
</>
Expand Down
7 changes: 2 additions & 5 deletions src/components/auth/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ type Status = {

type SignUpProps = {
onBackButtonClicked: () => void;
callback?: () => void;
};

export default function SignUp({ onBackButtonClicked, callback = () => {} }: SignUpProps) {
export default function SignUp({ onBackButtonClicked }: SignUpProps) {
const [formData, setFormData] = useState<FormData>({
nombre: '',
email: '',
Expand Down Expand Up @@ -123,9 +122,7 @@ export default function SignUp({ onBackButtonClicked, callback = () => {} }: Sig
// SIGN IN WITH NEW USER CREATED
await authService.signIn(formData.email, formData.password);
// REDIRECT USER TO HOME PAGE
if (typeof callback === 'function') {
callback();
}
window.location.href = '/';
};

return (
Expand Down
9 changes: 2 additions & 7 deletions src/components/auth/SocialButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@ import { ReactNode } from 'react';
type SocialButtonProps = {
provider: Provider;
children: ReactNode;
redirectUrl?: string;
};
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';
export default function SocialButton({ provider, children }: SocialButtonProps) {
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);
}
Expand Down