From 62daf4cc42545f5a1ad6c9216f5ea5a86d1304ed Mon Sep 17 00:00:00 2001 From: David Sharnoff Date: Thu, 29 Sep 2022 22:40:05 -0700 Subject: [PATCH] feat: add WithPath CookieHandlerOpt (#217) --- pkg/http/cookie.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/http/cookie.go b/pkg/http/cookie.go index 62ea295..4949b77 100644 --- a/pkg/http/cookie.go +++ b/pkg/http/cookie.go @@ -13,6 +13,7 @@ type CookieHandler struct { sameSite http.SameSite maxAge int domain string + path string } func NewCookieHandler(hashKey, encryptKey []byte, opts ...CookieHandlerOpt) *CookieHandler { @@ -20,6 +21,7 @@ func NewCookieHandler(hashKey, encryptKey []byte, opts ...CookieHandlerOpt) *Coo securecookie: securecookie.New(hashKey, encryptKey), secureOnly: true, sameSite: http.SameSiteLaxMode, + path: "/", } for _, opt := range opts { @@ -55,6 +57,12 @@ func WithDomain(domain string) CookieHandlerOpt { } } +func WithPath(path string) CookieHandlerOpt { + return func(c *CookieHandler) { + c.domain = path + } +} + func (c *CookieHandler) CheckCookie(r *http.Request, name string) (string, error) { cookie, err := r.Cookie(name) if err != nil { @@ -87,7 +95,7 @@ func (c *CookieHandler) SetCookie(w http.ResponseWriter, name, value string) err Name: name, Value: encoded, Domain: c.domain, - Path: "/", + Path: c.path, MaxAge: c.maxAge, HttpOnly: true, Secure: c.secureOnly, @@ -101,7 +109,7 @@ func (c *CookieHandler) DeleteCookie(w http.ResponseWriter, name string) { Name: name, Value: "", Domain: c.domain, - Path: "/", + Path: c.path, MaxAge: -1, HttpOnly: true, Secure: c.secureOnly,