Skip to content

Commit 66e227e

Browse files
committed
fixed redirect
1 parent 1c2705e commit 66e227e

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

src/contexts/AuthContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
100100
try {
101101
const { error } = await supabase.auth.signInWithPassword({ email, password });
102102
if (error) throw error;
103-
navigate('/');
103+
// Don't navigate here - let the Login component handle it via <Navigate>
104104
} catch (error: any) {
105105
toast({
106106
title: "Error signing in",
@@ -129,7 +129,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
129129
description: "Please check your email to verify your account.",
130130
});
131131

132-
navigate('/auth/login');
132+
// Don't navigate here - let the Register component handle it via <Navigate>
133133
} catch (error: any) {
134134
toast({
135135
title: "Error signing up",

src/pages/auth/Login.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import React from 'react';
33
import { useForm } from 'react-hook-form';
4-
import { Link, Navigate } from 'react-router-dom';
4+
import { Link, Navigate, useSearchParams } from 'react-router-dom';
55
import { Button } from '@/components/ui/button';
66
import { Input } from '@/components/ui/input';
77
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
@@ -18,6 +18,7 @@ type LoginFormValues = z.infer<typeof loginSchema>;
1818

1919
const Login = () => {
2020
const { user, signIn } = useAuth();
21+
const [searchParams] = useSearchParams();
2122
const form = useForm<LoginFormValues>({
2223
resolver: zodResolver(loginSchema),
2324
defaultValues: {
@@ -38,6 +39,14 @@ const Login = () => {
3839
};
3940

4041
if (user) {
42+
const redirect = searchParams.get('redirect');
43+
const showForm = searchParams.get('showForm');
44+
45+
if (redirect) {
46+
const redirectUrl = showForm ? `${redirect}?showForm=${showForm}` : redirect;
47+
return <Navigate to={redirectUrl} replace />;
48+
}
49+
4150
return <Navigate to="/" replace />;
4251
}
4352

@@ -102,7 +111,10 @@ const Login = () => {
102111

103112
<p className="mt-4 text-center text-sm text-muted-foreground">
104113
Don't have an account?{' '}
105-
<Link to="/auth/register" className="font-medium text-primary hover:text-accent">
114+
<Link
115+
to={`/auth/register${searchParams.toString() ? `?${searchParams.toString()}` : ''}`}
116+
className="font-medium text-primary hover:text-accent"
117+
>
106118
Sign up
107119
</Link>
108120
</p>

src/pages/auth/Register.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import React from 'react';
33
import { useForm } from 'react-hook-form';
4-
import { Link, Navigate } from 'react-router-dom';
4+
import { Link, Navigate, useSearchParams } from 'react-router-dom';
55
import { Button } from '@/components/ui/button';
66
import { Input } from '@/components/ui/input';
77
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
@@ -19,6 +19,7 @@ type RegisterFormValues = z.infer<typeof registerSchema>;
1919

2020
const Register = () => {
2121
const { user, signUp } = useAuth();
22+
const [searchParams] = useSearchParams();
2223
const form = useForm<RegisterFormValues>({
2324
resolver: zodResolver(registerSchema),
2425
defaultValues: {
@@ -40,6 +41,15 @@ const Register = () => {
4041
};
4142

4243
if (user) {
44+
// Check for redirect parameter
45+
const redirect = searchParams.get('redirect');
46+
const showForm = searchParams.get('showForm');
47+
48+
if (redirect) {
49+
const redirectUrl = showForm ? `${redirect}?showForm=${showForm}` : redirect;
50+
return <Navigate to={redirectUrl} replace />;
51+
}
52+
4353
return <Navigate to="/" replace />;
4454
}
4555

@@ -122,7 +132,10 @@ const Register = () => {
122132

123133
<p className="mt-4 text-center text-sm text-muted-foreground">
124134
Already have an account?{' '}
125-
<Link to="/auth/login" className="font-medium text-primary hover:text-accent">
135+
<Link
136+
to={`/auth/login${searchParams.toString() ? `?${searchParams.toString()}` : ''}`}
137+
className="font-medium text-primary hover:text-accent"
138+
>
126139
Sign in
127140
</Link>
128141
</p>

src/pages/categories/Architects.tsx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import React, { useEffect, useState } from "react";
1+
import React, { useEffect, useState, useRef } from "react";
22
import { supabase } from "@/integrations/supabase/client";
33
import { useAuth } from "@/contexts/AuthContext";
4-
import { useNavigate } from "react-router-dom";
4+
import { useNavigate, useSearchParams } from "react-router-dom";
55
import Navbar from "@/components/Navbar";
66
import Footer from "@/components/Footer";
77
import { Loader2, CheckCircle2, Smile } from "lucide-react";
88

99
const ArchitectsPage = () => {
1010
const { user } = useAuth();
1111
const navigate = useNavigate();
12+
const [searchParams, setSearchParams] = useSearchParams();
13+
const formRef = useRef<HTMLElement>(null);
1214
const [architects, setArchitects] = useState<any[]>([]);
1315
const [loading, setLoading] = useState(true);
1416
const [search, setSearch] = useState("");
@@ -56,6 +58,25 @@ const ArchitectsPage = () => {
5658
if (user) checkIfRegistered();
5759
}, [user]);
5860

61+
useEffect(() => {
62+
const shouldShowForm = searchParams.get("showForm");
63+
64+
if (shouldShowForm === "true" && user && !isAlreadyRegistered) {
65+
setShowForm(true);
66+
67+
const newSearchParams = new URLSearchParams(searchParams);
68+
newSearchParams.delete("showForm");
69+
setSearchParams(newSearchParams, { replace: true });
70+
71+
setTimeout(() => {
72+
formRef.current?.scrollIntoView({
73+
behavior: "smooth",
74+
block: "start"
75+
});
76+
}, 300);
77+
}
78+
}, [user, isAlreadyRegistered]);
79+
5980
// ✅ Handle input
6081
const handleChange = (
6182
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
@@ -179,11 +200,17 @@ const ArchitectsPage = () => {
179200

180201
const handleRegisterClick = () => {
181202
if (!user) {
182-
navigate("/auth/login");
203+
navigate("/auth/login?redirect=/categories/architects&showForm=true");
183204
} else if (isAlreadyRegistered) {
184205
setMessage("You are already registered as an architect.");
185206
} else {
186207
setShowForm(true);
208+
setTimeout(() => {
209+
formRef.current?.scrollIntoView({
210+
behavior: "smooth",
211+
block: "start"
212+
});
213+
}, 100);
187214
}
188215
};
189216

@@ -278,7 +305,10 @@ const ArchitectsPage = () => {
278305

279306
{/* Registration Form */}
280307
{showForm && user && !isAlreadyRegistered && (
281-
<section className="max-w-4xl mx-auto mt-10 bg-gray-50 p-6 rounded-2xl shadow-md border border-gray-200 relative">
308+
<section
309+
ref={formRef}
310+
className="max-w-4xl mx-auto mt-10 bg-gray-50 p-6 rounded-2xl shadow-md border border-gray-200 relative"
311+
>
282312
<button
283313
onClick={() => setShowForm(false)}
284314
className="absolute top-4 right-4 text-gray-600 hover:text-gray-800 text-3xl font-bold"

0 commit comments

Comments
 (0)