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 2ee41be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { UserContext } from "../provider/UserContext";
export const RedirectIfNotLoggedIn = ({ children }: PropsWithChildren) => {
const { user } = useContext(UserContext);

return !user?.uid ? <Navigate to="/login" replace /> : children;
const target = "/login" + `?redirect=${window.location.pathname}`;

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

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

return user?.uid ? <Navigate to="/dashboard" replace /> : children;
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 2ee41be

Please sign in to comment.