From 9367b1688fe86898aa91be9f443fc713af7b80df Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Tue, 5 Nov 2024 23:56:24 +0700 Subject: [PATCH 1/4] feat: auth with alby account before node setup --- config/config.go | 10 +--- .../src/components/redirects/HomeRedirect.tsx | 54 ++++++++++++------- frontend/src/constants.ts | 1 + frontend/src/routes.tsx | 12 +++-- frontend/src/screens/ConnectAlbyAccount.tsx | 8 ++- frontend/src/screens/Welcome.tsx | 50 +++++++++++------ frontend/src/screens/setup/SetupFinish.tsx | 21 ++------ 7 files changed, 90 insertions(+), 66 deletions(-) diff --git a/config/config.go b/config/config.go index ce6631932..5c378aaa7 100644 --- a/config/config.go +++ b/config/config.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "os" - "path" "github.com/getAlby/hub/db" "github.com/getAlby/hub/logger" @@ -117,19 +116,12 @@ func (cfg *config) init(env *AppConfig) error { } func (cfg *config) SetupCompleted() bool { - // TODO: remove AlbyUserIdentifier and hasLdkDir checks after 2025/01/01 - // to give time for users to update to 1.6.0+ - albyUserIdentifier, _ := cfg.Get("AlbyUserIdentifier", "") nodeLastStartTime, _ := cfg.Get("NodeLastStartTime", "") - ldkDir, err := os.Stat(path.Join(cfg.GetEnv().Workdir, "ldk")) - hasLdkDir := err == nil && ldkDir != nil && ldkDir.IsDir() logger.Logger.WithFields(logrus.Fields{ - "has_ldk_dir": hasLdkDir, - "has_alby_user_identifier": albyUserIdentifier != "", "has_node_last_start_time": nodeLastStartTime != "", }).Debug("Checking if setup is completed") - return albyUserIdentifier != "" || nodeLastStartTime != "" || hasLdkDir + return nodeLastStartTime != "" } func (cfg *config) GetJWTSecret() string { diff --git a/frontend/src/components/redirects/HomeRedirect.tsx b/frontend/src/components/redirects/HomeRedirect.tsx index 843952d92..b7459326b 100644 --- a/frontend/src/components/redirects/HomeRedirect.tsx +++ b/frontend/src/components/redirects/HomeRedirect.tsx @@ -13,31 +13,49 @@ export function HomeRedirect() { if (!info) { return; } + + const setupReturnTo = window.localStorage.getItem( + localStorageKeys.setupReturnTo + ); + let to: string | undefined; - if (info.setupCompleted && info.running) { - if (info.unlocked) { - if (info.albyAccountConnected || !info.albyUserIdentifier) { - const returnTo = window.localStorage.getItem( - localStorageKeys.returnTo - ); - // setTimeout hack needed for React strict mode (in development) - // because the effect runs twice before the navigation occurs - setTimeout(() => { - window.localStorage.removeItem(localStorageKeys.returnTo); - }, 100); - to = returnTo || "/home"; + if (!setupReturnTo) { + if (info.setupCompleted && info.running) { + if (info.unlocked) { + if (info.albyAccountConnected || !info.albyUserIdentifier) { + const returnTo = window.localStorage.getItem( + localStorageKeys.returnTo + ); + // setTimeout hack needed for React strict mode (in development) + // because the effect runs twice before the navigation occurs + setTimeout(() => { + window.localStorage.removeItem(localStorageKeys.returnTo); + }, 100); + to = returnTo || "/home"; + } else { + to = "/alby/auth"; + } } else { - to = "/alby/auth"; + to = "/unlock"; } + } else if (info.setupCompleted && !info.running) { + to = "/start"; + } else if (info.albyAccountConnected) { + to = "/welcome"; } else { - to = "/unlock"; + to = "/intro"; } - } else if (info.setupCompleted && !info.running) { - to = "/start"; } else { - to = "/intro"; + // setTimeout hack needed for React strict mode (in development) + // because the effect runs twice before the navigation occurs + setTimeout(() => { + window.localStorage.removeItem(localStorageKeys.setupReturnTo); + }, 100); + to = setupReturnTo; } - navigate(to); + navigate(to, { + replace: true, + }); }, [info, location, navigate]); if (!info) { diff --git a/frontend/src/constants.ts b/frontend/src/constants.ts index 28846f4fe..4c3f44664 100644 --- a/frontend/src/constants.ts +++ b/frontend/src/constants.ts @@ -1,5 +1,6 @@ export const localStorageKeys = { returnTo: "returnTo", + setupReturnTo: "setupReturnTo", channelOrder: "channelOrder", authToken: "authToken", }; diff --git a/frontend/src/routes.tsx b/frontend/src/routes.tsx index 96648661c..949e99bd7 100644 --- a/frontend/src/routes.tsx +++ b/frontend/src/routes.tsx @@ -358,6 +358,14 @@ const routes = [ { element: , }, + { + path: "alby", + element: , + }, + { + path: "auth", + element: , + }, { path: "password", element: , @@ -417,10 +425,6 @@ const routes = [ }, ], }, - { - path: "alby/auth", - element: , - }, ], }, { diff --git a/frontend/src/screens/ConnectAlbyAccount.tsx b/frontend/src/screens/ConnectAlbyAccount.tsx index e1e367df8..5c1bb0bda 100644 --- a/frontend/src/screens/ConnectAlbyAccount.tsx +++ b/frontend/src/screens/ConnectAlbyAccount.tsx @@ -16,7 +16,11 @@ import { CardTitle, } from "src/components/ui/card"; -export function ConnectAlbyAccount() { +type ConnectAlbyAccountProps = { + connectUrl?: string; +}; + +export function ConnectAlbyAccount({ connectUrl }: ConnectAlbyAccountProps) { return (
@@ -81,7 +85,7 @@ export function ConnectAlbyAccount() {
- + Connect now
@@ -37,26 +51,28 @@ export function Welcome() {

- + navigateToAuthPage( + info?.backendType + ? "/setup/password?node=preset" // node already setup through env variables + : "/setup/password?node=ldk" + ) + } > - - + Get Started + {info?.backendType && ` (${info?.backendType})`} + {info?.enableAdvancedSetup && ( - - - + )}
diff --git a/frontend/src/screens/setup/SetupFinish.tsx b/frontend/src/screens/setup/SetupFinish.tsx index 24bff60f6..8f6b651ba 100644 --- a/frontend/src/screens/setup/SetupFinish.tsx +++ b/frontend/src/screens/setup/SetupFinish.tsx @@ -5,7 +5,6 @@ import animationData from "src/assets/lotties/loading.json"; import Container from "src/components/Container"; import { Button } from "src/components/ui/button"; import { ToastSignature, useToast } from "src/components/ui/use-toast"; -import { localStorageKeys } from "src/constants"; import { useInfo } from "src/hooks/useInfo"; import { saveAuthToken } from "src/lib/auth"; @@ -17,7 +16,6 @@ import { request } from "src/utils/request"; let lastStartupErrorTime: string; export function SetupFinish() { const navigate = useNavigate(); - const { nodeInfo, unlockPassword } = useSetupStore(); const { toast } = useToast(); const { data: info } = useInfo(true); // poll the info endpoint to auto-redirect when app is running @@ -86,10 +84,9 @@ export function SetupFinish() { (async () => { setLoading(true); const succeeded = await finishSetup( - nodeInfo, - unlockPassword, - toast, - info.oauthRedirect + useSetupStore.getState().nodeInfo, + useSetupStore.getState().unlockPassword, + toast ); // only setup call is successful as start is async if (!succeeded) { @@ -97,7 +94,7 @@ export function SetupFinish() { setConnectionError(true); } })(); - }, [nodeInfo, navigate, unlockPassword, toast, info]); + }, [navigate, toast, info]); if (connectionError) { return ( @@ -134,17 +131,9 @@ export function SetupFinish() { const finishSetup = async ( nodeInfo: SetupNodeInfo, unlockPassword: string, - toast: ToastSignature, - autoAuth: boolean + toast: ToastSignature ): Promise => { try { - let redirectTo = "/alby/account"; - if (autoAuth) { - redirectTo = "/alby/auth"; - } - - window.localStorage.setItem(localStorageKeys.returnTo, redirectTo); - await request("/api/setup", { method: "POST", headers: { From 42331ab36619019436cb4faf9acc27be219efdbf Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Wed, 6 Nov 2024 00:10:23 +0700 Subject: [PATCH 2/4] fix: redirect to auth page from start page if auth is needed --- frontend/src/components/redirects/StartRedirect.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/redirects/StartRedirect.tsx b/frontend/src/components/redirects/StartRedirect.tsx index 15b05c106..e3d42aba9 100644 --- a/frontend/src/components/redirects/StartRedirect.tsx +++ b/frontend/src/components/redirects/StartRedirect.tsx @@ -1,7 +1,7 @@ -import { useInfo } from "src/hooks/useInfo"; -import { useLocation, useNavigate } from "react-router-dom"; import React from "react"; +import { useLocation, useNavigate } from "react-router-dom"; import Loading from "src/components/Loading"; +import { useInfo } from "src/hooks/useInfo"; export function StartRedirect({ children }: React.PropsWithChildren) { const { data: info } = useInfo(); @@ -10,8 +10,12 @@ export function StartRedirect({ children }: React.PropsWithChildren) { React.useEffect(() => { if (!info || (info.setupCompleted && !info.running)) { + if (info && !info.albyAccountConnected && info.albyUserIdentifier) { + navigate("/alby/auth"); + } return; } + navigate("/"); }, [info, location, navigate]); From b14b529575a32f5a058a8e1e70c4ff7c2687c4e0 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 7 Nov 2024 19:04:36 +0700 Subject: [PATCH 3/4] fix: do not show auth page twice if user goes back after authenticating in setup --- frontend/src/components/redirects/HomeRedirect.tsx | 2 ++ frontend/src/screens/Welcome.tsx | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/frontend/src/components/redirects/HomeRedirect.tsx b/frontend/src/components/redirects/HomeRedirect.tsx index b7459326b..a2f1ed240 100644 --- a/frontend/src/components/redirects/HomeRedirect.tsx +++ b/frontend/src/components/redirects/HomeRedirect.tsx @@ -41,6 +41,8 @@ export function HomeRedirect() { } else if (info.setupCompleted && !info.running) { to = "/start"; } else if (info.albyAccountConnected) { + // in case user goes back after authenticating in setup + // we don't want to show the intro twice to = "/welcome"; } else { to = "/intro"; diff --git a/frontend/src/screens/Welcome.tsx b/frontend/src/screens/Welcome.tsx index 4ba8f9253..7bf7ee32b 100644 --- a/frontend/src/screens/Welcome.tsx +++ b/frontend/src/screens/Welcome.tsx @@ -26,6 +26,13 @@ export function Welcome() { }, [info, navigate]); function navigateToAuthPage(returnTo: string) { + if (info?.albyAccountConnected) { + // in case user goes back after authenticating in setup + // we don't want to show the auth screen twice + navigate(returnTo); + return; + } + window.localStorage.setItem(localStorageKeys.setupReturnTo, returnTo); // by default, allow the user to choose whether or not to connect to alby account From b952ea8a4f73efa1d1a157a846339dbb4cebfa68 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 7 Nov 2024 19:15:16 +0700 Subject: [PATCH 4/4] fix: re-add ldk directory check to calculate if setup has completed --- config/config.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 5c378aaa7..7b7effdbe 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "os" + "path" "github.com/getAlby/hub/db" "github.com/getAlby/hub/logger" @@ -116,12 +117,17 @@ func (cfg *config) init(env *AppConfig) error { } func (cfg *config) SetupCompleted() bool { + // TODO: remove hasLdkDir check after 2025/01/01 + // to give time for users to update to 1.6.0+ nodeLastStartTime, _ := cfg.Get("NodeLastStartTime", "") + ldkDir, err := os.Stat(path.Join(cfg.GetEnv().Workdir, "ldk")) + hasLdkDir := err == nil && ldkDir != nil && ldkDir.IsDir() logger.Logger.WithFields(logrus.Fields{ + "has_ldk_dir": hasLdkDir, "has_node_last_start_time": nodeLastStartTime != "", }).Debug("Checking if setup is completed") - return nodeLastStartTime != "" + return nodeLastStartTime != "" || hasLdkDir } func (cfg *config) GetJWTSecret() string {