Skip to content

Commit

Permalink
Feat: add logout route for http mode (#502)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rolznz authored Aug 22, 2024
1 parent 4674ee3 commit fe00740
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
14 changes: 9 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions config/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/components/layouts/AppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();
Expand All @@ -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");

Expand Down
4 changes: 4 additions & 0 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export default defineConfig(({ command }) => ({
target: "http://localhost:8080",
secure: false,
},
"/logout": {
target: "http://localhost:8080",
secure: false,
},
},
},
resolve: {
Expand Down
6 changes: 6 additions & 0 deletions http/alby_http_service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"errors"
"fmt"
"net/http"

Expand Down Expand Up @@ -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)
}

Expand Down
13 changes: 13 additions & 0 deletions http/http_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fe00740

Please sign in to comment.