feat: add WithPath CookieHandlerOpt (#217)

This commit is contained in:
David Sharnoff 2022-09-29 22:40:05 -07:00 committed by GitHub
parent 328d0e1251
commit 62daf4cc42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,6 +13,7 @@ type CookieHandler struct {
sameSite http.SameSite sameSite http.SameSite
maxAge int maxAge int
domain string domain string
path string
} }
func NewCookieHandler(hashKey, encryptKey []byte, opts ...CookieHandlerOpt) *CookieHandler { 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), securecookie: securecookie.New(hashKey, encryptKey),
secureOnly: true, secureOnly: true,
sameSite: http.SameSiteLaxMode, sameSite: http.SameSiteLaxMode,
path: "/",
} }
for _, opt := range opts { 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) { func (c *CookieHandler) CheckCookie(r *http.Request, name string) (string, error) {
cookie, err := r.Cookie(name) cookie, err := r.Cookie(name)
if err != nil { if err != nil {
@ -87,7 +95,7 @@ func (c *CookieHandler) SetCookie(w http.ResponseWriter, name, value string) err
Name: name, Name: name,
Value: encoded, Value: encoded,
Domain: c.domain, Domain: c.domain,
Path: "/", Path: c.path,
MaxAge: c.maxAge, MaxAge: c.maxAge,
HttpOnly: true, HttpOnly: true,
Secure: c.secureOnly, Secure: c.secureOnly,
@ -101,7 +109,7 @@ func (c *CookieHandler) DeleteCookie(w http.ResponseWriter, name string) {
Name: name, Name: name,
Value: "", Value: "",
Domain: c.domain, Domain: c.domain,
Path: "/", Path: c.path,
MaxAge: -1, MaxAge: -1,
HttpOnly: true, HttpOnly: true,
Secure: c.secureOnly, Secure: c.secureOnly,