Skip to content

Commit

Permalink
feat: correct auth redirect target
Browse files Browse the repository at this point in the history
  • Loading branch information
thraizz committed Mar 9, 2024
1 parent 77ccaa2 commit 86c94ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Authentication } from "./views/Authentication";
import {
RedirectIfNotLoggedIn,
RedirectIfSignedIn,
} from "./utils/RedirectIfNotLoggedIn";
} from "./utils/AuthRedirects";
import { Goals } from "./views/Goals";
import { NewLayout } from "./views/NewLayout";
import { routes } from "./routes";
Expand Down
21 changes: 21 additions & 0 deletions src/utils/AuthRedirects.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PropsWithChildren, useContext } from "react";
import { Navigate } from "react-router-dom";
import { UserContext } from "../provider/UserContext";

export const RedirectIfNotLoggedIn = ({ children }: PropsWithChildren) => {
const { user } = useContext(UserContext);

const target = "/login" + `?redirect=${window.location.pathname}`;

return !user?.uid ? <Navigate to={target} replace /> : children;
};

export const RedirectIfSignedIn = ({ children }: PropsWithChildren) => {
const { user } = useContext(UserContext);

const query = new URLSearchParams(window.location.search);

const target = query.get("redirect") || "/dashboard";

return user?.uid ? <Navigate to={target} replace /> : children;
};

0 comments on commit 86c94ac

Please sign in to comment.