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
This commit is contained in:
Roland 2024-08-22 14:25:09 +07:00 committed by GitHub
parent 4674ee3d3d
commit fe007409ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 43 additions and 13 deletions

View file

@ -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

View file

@ -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"`

View file

@ -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]]

View file

@ -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");

View file

@ -52,6 +52,10 @@ export default defineConfig(({ command }) => ({
target: "http://localhost:8080",
secure: false,
},
"/logout": {
target: "http://localhost:8080",
secure: false,
},
},
},
resolve: {

View file

@ -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)
}

View file

@ -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 {