Skip to content

Commit

Permalink
Redirect to previous page on login
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteStash committed Sep 1, 2024
1 parent 52d8823 commit 75d5611
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 5 additions & 2 deletions frontend/src/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const Login: FC = () => {
const navigate = useNavigate();
const [loginError, setLoginError] = useState("");
const msg = new URLSearchParams(location.search).get("msg");
const redirect = new URLSearchParams(location.search).get("redirect");
const Auth = useContext<ContextType>(AuthContext);
const {
register,
Expand All @@ -49,8 +50,10 @@ const Login: FC = () => {
method: "POST",
body,
credentials: getCredentialsSetting(),
}).finally(() => setLoading(false));
if (res.ok) window.location.replace("/");
});

const returnURL = decodeURIComponent(redirect ?? "") || "/";
if (res.ok) window.location.replace(returnURL);
else setLoginError("Access denied");
};

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ const Main: FC<Props> = ({ children }) => {
location.pathname !== ROUTE_FORGOT_PASSWORD &&
location.pathname !== ROUTE_RESET_PASSWORD
) {
navigate(ROUTE_LOGIN);
navigate(
`${ROUTE_LOGIN}?redirect=${encodeURIComponent(location.pathname)}`
);
}
}, [loading, user, location, navigate]);

Expand Down

0 comments on commit 75d5611

Please sign in to comment.