Skip to content

Commit 6109d3f

Browse files
committed
routing after authrntication changed to dashboard from /home
1 parent d7862b3 commit 6109d3f

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

Client/src/app/router.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import LoginPage from "../pages/LoginPage";
66
import ProtectedRoute from "../components/ProtectedRoute";
77
import DashBoard from "../pages/DashBoard";
88
import SignupPage from "../pages/SignupPage";
9+
import RedirectIfLoggedIn from "../components/RedirectIfLoggedIn";
910

1011
const router = createBrowserRouter([
1112
{
@@ -14,17 +15,27 @@ const router = createBrowserRouter([
1415
children: [
1516
{
1617
index: true,
18+
element: <HomePage />, // public
19+
},
20+
21+
{
22+
path: "login",
1723
element: (
18-
<ProtectedRoute>
19-
<HomePage />
20-
</ProtectedRoute>
24+
<RedirectIfLoggedIn>
25+
<LoginPage />
26+
</RedirectIfLoggedIn>
2127
),
2228
},
29+
2330
{
24-
path: "Login",
25-
element: <LoginPage />,
31+
path: "register",
32+
element: (
33+
<RedirectIfLoggedIn>
34+
<SignupPage />
35+
</RedirectIfLoggedIn>
36+
),
2637
},
27-
{ path: "Register", element: <SignupPage /> },
38+
2839
{
2940
path: "dashboard",
3041
element: (
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Navigate } from "react-router-dom";
2+
import { useAuthUser } from "../auth/auth.store";
3+
interface ProtectedRouteProps {
4+
children: React.ReactNode;
5+
}
6+
const RedirectIfLoggedIn: React.FC<ProtectedRouteProps> = ({ children }) => {
7+
const user = useAuthUser();
8+
if (user) {
9+
return <Navigate to="/dashboard" replace />;
10+
}
11+
12+
return children;
13+
};
14+
15+
export default RedirectIfLoggedIn;

Client/src/components/login-form.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ export function LoginForm({
3636
const navigate = useNavigate();
3737
const user = useAuthUser();
3838
const location = useLocation();
39-
const fromPath = location.state?.from?.pathname || "/";
39+
const fromPath =
40+
location.state?.from?.pathname &&
41+
location.state?.from?.pathname !== "/login"
42+
? location.state.from.pathname
43+
: "/dashboard";
44+
4045
const [email, setEmail] = useState("");
4146
const [password, setPassword] = useState("");
4247
if (user) {

Client/src/components/signup-form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function SignupForm({
5353

5454
try {
5555
await register({ name, email, password });
56-
navigate("/", { replace: true });
56+
navigate("/dashboard", { replace: true });
5757
} catch (err) {
5858
console.log("Registration failed on frontend side.", err);
5959
}

0 commit comments

Comments
 (0)