Skip to content

Commit

Permalink
feat: add logout route
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Aug 16, 2024
1 parent ef5d86b commit e9b739b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions frontend/src/components/layouts/AppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ export default function AppLayout() {
const logout = React.useCallback(async () => {
deleteAuthToken();
await refetchInfo();
navigate("/", { replace: true });
toast({ title: "You are now logged out." });

const isHttpMode = window.location.protocol.startsWith("http");
if (isHttpMode) {
window.location.href = "/logout";
} else {
navigate("/", { replace: true });
toast({ title: "You are now logged out." });
}
}, [navigate, refetchInfo, toast]);

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
14 changes: 14 additions & 0 deletions http/http_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (httpSvc *HttpService) RegisterSharedRoutes(e *echo.Echo) {
XFrameOptions: "DENY",
ContentSecurityPolicy: "default-src 'self'; img-src 'self' https://uploads.getalby-assets.com https://getalby.com;",
ReferrerPolicy: "no-referrer",
Skipper: func(c echo.Context) bool {
// only serve the react app on the root
return c.Path() != "/"
},
}))
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
Expand Down Expand Up @@ -97,6 +101,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 +941,15 @@ 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
}

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 e9b739b

Please sign in to comment.