summaryrefslogtreecommitdiff
path: root/pkg/web/cookie.go
blob: 11cc8074a261844d16349c4e14cb9b7214e7a335 (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
32
33
34
35
package web

import (
	"net/http"

	"github.com/xlgmokha/x/pkg/cookie"
	"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),
		cookie.WithPath("/"),
		cookie.WithHttpOnly(true),
		cookie.WithSecure(true),
	)...)
}

func ExpireCookie(w http.ResponseWriter, name string) error {
	return WriteCookie(w, cookie.Reset(name,
		cookie.WithPath("/"),
		cookie.WithHttpOnly(true),
		cookie.WithSecure(true),
	))
}

func WriteCookie(w http.ResponseWriter, c *http.Cookie) error {
	if err := c.Valid(); err != nil {
		return err
	}
	cookie.Write(w, c)
	return nil
}