diff --git a/server/router/handler/logout.go b/server/router/handler/logout.go index 479b5e8..279cf1c 100644 --- a/server/router/handler/logout.go +++ b/server/router/handler/logout.go @@ -18,7 +18,7 @@ func HandleLogout(sc context.ServerContext) echo.HandlerFunc { ) if cookie, err = c.Cookie("session"); err != nil { // cookie not found - return c.Redirect(http.StatusSeeOther, "/") + return c.JSON(http.StatusNotFound, map[string]string{"reason": "session not found"}) } sessionId = cookie.Value if err = sc.Db.DeleteSession(&db.Session{SessionId: sessionId}); err != nil { @@ -26,6 +26,6 @@ func HandleLogout(sc context.ServerContext) echo.HandlerFunc { } // tell browser that cookie is expired and thus can be deleted c.SetCookie(&http.Cookie{Name: "session", HttpOnly: true, Path: "/", Value: sessionId, Secure: true, Expires: time.Now()}) - return c.Redirect(http.StatusSeeOther, "/") + return c.JSON(http.StatusSeeOther, map[string]string{"status": "OK"}) } } diff --git a/server/router/router.go b/server/router/router.go index 74408e2..7c05b6f 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -25,7 +25,6 @@ func mountMiddleware(e *echo.Echo, sc ServerContext) { func addFrontendRoutes(e *echo.Echo, sc ServerContext) { GET(e, sc, "/", handler.HandleIndex) - POST(e, sc, "/logout", handler.HandleLogout) GET(e, sc, "/user", handler.HandleUser, middleware.SessionGuard) @@ -44,6 +43,7 @@ func addFrontendRoutes(e *echo.Echo, sc ServerContext) { func addBackendRoutes(e *echo.Echo, sc ServerContext) { GET(e, sc, "/api/login", handler.HandleLogin) GET(e, sc, "/api/login/callback", handler.HandleLoginCallback) + POST(e, sc, "/api/logout", handler.HandleLogout) GET(e, sc, "/api/session", handler.HandleCheckSession) GET(e, sc, "/api/invoice/:id", handler.HandleInvoiceStatus, diff --git a/vue/src/components/UserView.vue b/vue/src/components/UserView.vue index fffa6af..6e94225 100644 --- a/vue/src/components/UserView.vue +++ b/vue/src/components/UserView.vue @@ -1,8 +1,32 @@ + + diff --git a/vue/src/stores/session.js b/vue/src/stores/session.js index 90f5c1b..9941da5 100644 --- a/vue/src/stores/session.js +++ b/vue/src/stores/session.js @@ -37,5 +37,16 @@ export const useSession = defineStore('session', () => { return fetch(url, { credentials: 'include' }).then(r => r.json()) } - return { pubkey, isAuthenticated, initialized, init, checkSession, login } + function logout () { + const url = window.origin + '/api/logout' + return fetch(url, { method: 'POST', credentials: 'include' }) + .then(async r => { + const body = await r.json() + if (body.status === 'OK') { + pubkey.value = null + } + }) + } + + return { pubkey, isAuthenticated, initialized, init, checkSession, login, logout } })