diff options
Diffstat (limited to 'pkg/web/cookie.go')
| -rw-r--r-- | pkg/web/cookie.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/pkg/web/cookie.go b/pkg/web/cookie.go new file mode 100644 index 0000000..d4b1555 --- /dev/null +++ b/pkg/web/cookie.go @@ -0,0 +1,33 @@ +package web + +import ( + "net/http" + + "github.com/xlgmokha/x/pkg/cookie" + "github.com/xlgmokha/x/pkg/env" + "github.com/xlgmokha/x/pkg/x" +) + +func NewCookie(name, value string, options ...x.Option[*http.Cookie]) *http.Cookie { + options = x.Prepend[x.Option[*http.Cookie]]( + options, + cookie.WithName(name), + cookie.WithValue(value), // TODO:: digitally sign the value + cookie.WithPath("/"), + cookie.WithHttpOnly(true), + cookie.WithSecure(true), + cookie.WithSameSite(http.SameSiteDefaultMode), + cookie.WithDomain(env.Fetch("HOST", "localhost")), + ) + + return x.New[*http.Cookie](options...) +} + +func ExpireCookie(w http.ResponseWriter, name string) { + cookie.Expire(w, name, + cookie.WithPath("/"), + cookie.WithDomain(env.Fetch("HOST", "localhost")), + cookie.WithHttpOnly(true), + cookie.WithSecure(true), + ) +} |
