From fe007409caa028415e50ea6567c45bf448aef30e Mon Sep 17 00:00:00 2001 From: Roland <33993199+rolznz@users.noreply.github.com> Date: Thu, 22 Aug 2024 14:25:09 +0700 Subject: [PATCH] Feat: add logout route for http mode (#502) * feat: add logout route * fix: remove skipper * chore: remove unnecessary toast * fix: do not have a default BASE_URL * fix: default log level to be info instead of debug --- .env.example | 14 +++++++++----- config/models.go | 4 ++-- fly.toml | 2 +- frontend/src/components/layouts/AppLayout.tsx | 13 ++++++++----- frontend/vite.config.ts | 4 ++++ http/alby_http_service.go | 6 ++++++ http/http_service.go | 13 +++++++++++++ 7 files changed, 43 insertions(+), 13 deletions(-) diff --git a/.env.example b/.env.example index 5b7b40df..d06a48f9 100644 --- a/.env.example +++ b/.env.example @@ -4,10 +4,15 @@ LOG_EVENTS=false # do not link your current account when you run a dev instance (so it stays pointing at your mainnet one) AUTO_LINK_ALBY_ACCOUNT=false -# Optionally LDK debug log level to get more info +# Optionally set LDK debug log level to get more info #LDK_LOG_LEVEL=2 -# Logrus debug log level -LOG_LEVEL=4 +# Optionally set Main application debug log level to get more info +#LOG_LEVEL=5 + +# Base URL required for custom OAuth client +#BASE_URL=http://localhost:8080 +# Development settings (yarn dev:http) +FRONTEND_URL=http://localhost:5173 #WORK_DIR=.data #DATABASE_URI=nwc.db @@ -16,13 +21,12 @@ LOG_LEVEL=4 #RELAY=wss://relay.getalby.com/v1 #RELAY=ws://localhost:7447/v1 #PORT=8080 -#FRONTEND_URL=http://localhost:5173 # Alby OAuth configuration #ALBY_OAUTH_CLIENT_SECRET= #ALBY_OAUTH_CLIENT_ID= -#BASE_URL= + # Polar LND Client #LN_BACKEND_TYPE=LND diff --git a/config/models.go b/config/models.go index 0ba7d130..4383ea25 100644 --- a/config/models.go +++ b/config/models.go @@ -23,7 +23,7 @@ type AppConfig struct { Port string `envconfig:"PORT" default:"8080"` DatabaseUri string `envconfig:"DATABASE_URI" default:"nwc.db"` JWTSecret string `envconfig:"JWT_SECRET"` - LogLevel string `envconfig:"LOG_LEVEL"` + LogLevel string `envconfig:"LOG_LEVEL" default:"4"` LDKNetwork string `envconfig:"LDK_NETWORK" default:"bitcoin"` LDKEsploraServer string `envconfig:"LDK_ESPLORA_SERVER" default:"https://electrs.getalbypro.com"` // TODO: remove LDK prefix LDKGossipSource string `envconfig:"LDK_GOSSIP_SOURCE"` @@ -33,7 +33,7 @@ type AppConfig struct { AlbyClientId string `envconfig:"ALBY_OAUTH_CLIENT_ID" default:"J2PbXS1yOf"` AlbyClientSecret string `envconfig:"ALBY_OAUTH_CLIENT_SECRET" default:"rABK2n16IWjLTZ9M1uKU"` AlbyOAuthAuthUrl string `envconfig:"ALBY_OAUTH_AUTH_URL" default:"https://getalby.com/oauth"` - BaseUrl string `envconfig:"BASE_URL" default:"http://localhost:8080"` + BaseUrl string `envconfig:"BASE_URL"` FrontendUrl string `envconfig:"FRONTEND_URL"` LogEvents bool `envconfig:"LOG_EVENTS" default:"true"` AutoLinkAlbyAccount bool `envconfig:"AUTO_LINK_ALBY_ACCOUNT" default:"true"` diff --git a/fly.toml b/fly.toml index bd119fb4..9bb1f1df 100644 --- a/fly.toml +++ b/fly.toml @@ -13,7 +13,7 @@ swap_size_mb = 2048 [env] DATABASE_URI = '/data/nwc.db' LDK_LOG_LEVEL = '3' - LOG_LEVEL = '5' + LOG_LEVEL = '4' WORK_DIR = '/data' [[mounts]] diff --git a/frontend/src/components/layouts/AppLayout.tsx b/frontend/src/components/layouts/AppLayout.tsx index 303aa851..faed3812 100644 --- a/frontend/src/components/layouts/AppLayout.tsx +++ b/frontend/src/components/layouts/AppLayout.tsx @@ -44,7 +44,6 @@ import { TooltipProvider, TooltipTrigger, } from "src/components/ui/tooltip"; -import { useToast } from "src/components/ui/use-toast"; import { useAlbyMe } from "src/hooks/useAlbyMe"; import { useInfo } from "src/hooks/useInfo"; @@ -58,7 +57,6 @@ export default function AppLayout() { const { data: albyMe } = useAlbyMe(); const { data: info, mutate: refetchInfo } = useInfo(); - const { toast } = useToast(); const [mobileMenuOpen, setMobileMenuOpen] = React.useState(false); const location = useLocation(); const navigate = useNavigate(); @@ -71,9 +69,14 @@ export default function AppLayout() { const logout = React.useCallback(async () => { deleteAuthToken(); await refetchInfo(); - navigate("/", { replace: true }); - toast({ title: "You are now logged out." }); - }, [navigate, refetchInfo, toast]); + + const isHttpMode = window.location.protocol.startsWith("http"); + if (isHttpMode) { + window.location.href = "/logout"; + } else { + navigate("/", { replace: true }); + } + }, [navigate, refetchInfo]); const isHttpMode = window.location.protocol.startsWith("http"); diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index d52c9f67..a24b46b5 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -52,6 +52,10 @@ export default defineConfig(({ command }) => ({ target: "http://localhost:8080", secure: false, }, + "/logout": { + target: "http://localhost:8080", + secure: false, + }, }, }, resolve: { diff --git a/http/alby_http_service.go b/http/alby_http_service.go index 06ccc984..74153ee4 100644 --- a/http/alby_http_service.go +++ b/http/alby_http_service.go @@ -1,6 +1,7 @@ package http import ( + "errors" "fmt" "net/http" @@ -93,6 +94,11 @@ func (albyHttpSvc *AlbyHttpService) albyCallbackHandler(c echo.Context) error { redirectUrl = albyHttpSvc.appConfig.BaseUrl } + if redirectUrl == "" { + // OAuth using a custom client requires a base URL set for the callback + return errors.New("no BASE_URL set") + } + return c.Redirect(http.StatusFound, redirectUrl) } diff --git a/http/http_service.go b/http/http_service.go index 3e6695d3..b5ae1c8d 100644 --- a/http/http_service.go +++ b/http/http_service.go @@ -97,6 +97,7 @@ func (httpSvc *HttpService) RegisterSharedRoutes(e *echo.Echo) { e.POST("/api/unlock", httpSvc.unlockHandler, unlockRateLimiter) e.PATCH("/api/unlock-password", httpSvc.changeUnlockPasswordHandler, unlockRateLimiter) e.POST("/api/backup", httpSvc.createBackupHandler, unlockRateLimiter) + e.GET("/logout", httpSvc.logoutHandler, unlockRateLimiter) frontend.RegisterHandlers(e) @@ -936,6 +937,18 @@ func (httpSvc *HttpService) getLogOutputHandler(c echo.Context) error { return c.JSON(http.StatusOK, getLogResponse) } +func (httpSvc *HttpService) logoutHandler(c echo.Context) error { + redirectUrl := httpSvc.cfg.GetEnv().FrontendUrl + if redirectUrl == "" { + redirectUrl = httpSvc.cfg.GetEnv().BaseUrl + } + if redirectUrl == "" { + redirectUrl = "/" + } + + return c.Redirect(http.StatusFound, redirectUrl) +} + func (httpSvc *HttpService) createBackupHandler(c echo.Context) error { var backupRequest api.BasicBackupRequest if err := c.Bind(&backupRequest); err != nil {