From 700f2e4090bfe5ed08f35d3df7733d354b2365bf Mon Sep 17 00:00:00 2001 From: ae Date: Wed, 2 Apr 2025 12:43:32 +0300 Subject: [PATCH] fix: clear the rt cookie on logout --- server/pkg/service/tokens.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/server/pkg/service/tokens.go b/server/pkg/service/tokens.go index 8316b80..758badf 100644 --- a/server/pkg/service/tokens.go +++ b/server/pkg/service/tokens.go @@ -53,6 +53,13 @@ type tokensResource struct { func (rs tokensResource) Routes() chi.Router { r := chi.NewRouter() + // Protected routes (access token required) + r.Group(func(r chi.Router) { + r.Use(requireAccessToken(rs.JWTSecret)) + r.Post("/logout", rs.HandleLogout) // POST /auth/logout - revoke all refresh cookies + }) + + // Protected routes (refresh token required) r.Group(func(r chi.Router) { r.Use(requireRefreshToken(rs.JWTSecret)) r.Post("/refresh", rs.RefreshAccessToken) // POST /auth/refresh - convert refresh token to new token pair @@ -175,12 +182,23 @@ func (rs tokensResource) HandleLogout(w http.ResponseWriter, r *http.Request) { return } + // Clear the refresh token cookie + http.SetCookie(w, &http.Cookie{ + Name: "refresh_token", + Value: "", + Path: "/", + MaxAge: -1, // Expires immediately + HttpOnly: true, + Secure: true, + SameSite: http.SameSiteStrictMode, + }) + if err := rs.Tokens.RevokeAllUserRefreshTokens(r.Context(), userID); err != nil { respondError(w, http.StatusInternalServerError, "Failed to logout") return } - respondJSON(w, http.StatusOK, map[string]string{"status": "logged out"}) + w.WriteHeader(http.StatusNoContent) } func getTokenFromRequest(r *http.Request) (string, error) {