Skip to content

Commit

Permalink
fix: redirect after logging in
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jan 31, 2024
1 parent 6320e36 commit f140c10
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
5 changes: 4 additions & 1 deletion frontend/src/components/redirects/AppsRedirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { useInfo } from "src/hooks/useInfo";
import { Outlet, useLocation, useNavigate } from "react-router-dom";
import React from "react";
import Loading from "src/components/Loading";
import { localStorageKeys } from "src/constants";

export function AppsRedirect() {
const { data: info } = useInfo();
const location = useLocation();
const navigate = useNavigate();

// TODO: re-add login redirect: https://github.com/getAlby/nostr-wallet-connect/commit/59b041886098dda4ff38191e3dd704ec36360673
React.useEffect(() => {
if (!info || (info.running && info.unlocked)) {
window.localStorage.removeItem(localStorageKeys.returnTo);
return;
}
const returnTo = location.pathname + location.search;
window.localStorage.setItem(localStorageKeys.returnTo, returnTo);
navigate("/");
}, [info, location, navigate]);

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/redirects/HomeRedirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useInfo } from "src/hooks/useInfo";
import { useLocation, useNavigate } from "react-router-dom";
import React from "react";
import Loading from "src/components/Loading";
import { localStorageKeys } from "src/constants";

export function HomeRedirect() {
const { data: info } = useInfo();
Expand All @@ -15,7 +16,8 @@ export function HomeRedirect() {
let to: string | undefined;
if (info.setupCompleted && info.running) {
if (info.unlocked) {
to = "/apps";
const returnTo = window.localStorage.getItem(localStorageKeys.returnTo);
to = returnTo || "/apps";
} else {
to = "/unlock";
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const localStorageKeys = {
returnTo: "returnTo",
};
2 changes: 1 addition & 1 deletion frontend/src/screens/Start.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function Start() {
console.log({ res });
await refetchInfo();

navigate("/apps");
navigate("/");
} catch (error) {
handleRequestError("Failed to connect", error);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/Unlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function Unlock() {
});
console.log({ res });
await refetchInfo();
navigate("/apps");
navigate("/");
} catch (error) {
handleRequestError("Failed to connect", error);
} finally {
Expand Down

0 comments on commit f140c10

Please sign in to comment.