Skip to content

Commit

Permalink
Save path state after login (#231)
Browse files Browse the repository at this point in the history
* Fix redirect issue in AuthProvider

* Add correct logic for redirecting

* use sessionStorage instead

* Format

---------

Co-authored-by: Jason Zheng <jasonz4200@gmail.com>
  • Loading branch information
akinfelami and jasozh authored Apr 16, 2024
1 parent 8e98917 commit 966e6c8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
7 changes: 0 additions & 7 deletions frontend/src/components/organisms/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ const LoginForm = () => {
await signInWithEmailAndPassword(email, password);
};

/** Handles login success */
useEffect(() => {
if (signedInUser?.user.emailVerified) {
router.push("/events/view");
}
}, [signedInUser]);

/** Handles login errors */
const [errorMessage, setErrorMessage] = useState("");
useEffect(() => {
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/components/organisms/ViewEvents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ const UpcomingEvents = () => {
/** Error screen */
if (isError) return <FetchDataError />;

console.log(upcomingEventsSupervisor);

return (
<div>
{(role === "Supervisor" || role === "Admin") && (
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/utils/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const AuthProvider = ({ children }: AuthProviderProps) => {
/^\/events\/[a-zA-Z0-9_-]+\/(attendees|edit)|\/events\/create$/;

const regexMatcherforAdminPaths =
/^\/users\/([a-zA-Z0-9_-]+)\/(manage|view)$/
/^\/users\/([a-zA-Z0-9_-]+)\/(manage|view)$/;
// check auth state
if (user) {
const { claims } = await user.getIdTokenResult();
Expand All @@ -172,9 +172,17 @@ export const AuthProvider = ({ children }: AuthProviderProps) => {
}

if (!user && !publicPaths.includes(path) && !isResetPage(path)) {
sessionStorage.setItem("redirectPath", path);
router.replace("/login");
} else if (user && user.emailVerified && authPaths.includes(path)) {
router.replace("/events/view");
const redirectPath = sessionStorage.getItem("redirectPath");
if (redirectPath) {
router
.replace(redirectPath)
.then(() => sessionStorage.removeItem("redirectPath"));
} else {
router.replace("/events/view");
}
} else if (
user &&
user.emailVerified &&
Expand Down

0 comments on commit 966e6c8

Please sign in to comment.