summaryrefslogtreecommitdiff
path: root/pkg/web/cookie.go
blob: 1377274b86d8c8c89e26fc90a7d802c99cb9ee3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 {
	return x.New[*http.Cookie](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")),
	)...)
}

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),
	)
}