1
0
Fork 0
eighty/cookie.go

54 lines
1.2 KiB
Go
Raw Normal View History

2022-03-25 06:06:43 +09:00
package eighty
import (
"net/http"
"time"
)
var (
oldTime = time.Unix(0, 0)
)
// SetCookieValue is a useful cookie generator for net.http.
func SetCookieValue(key string, expireDuration time.Duration, sessionSecure bool) func(http.ResponseWriter, string, string) {
return func(w http.ResponseWriter, host, newCookieValue string) {
if len(newCookieValue) > 0 {
http.SetCookie(w,
&http.Cookie{
Name: key,
Value: newCookieValue,
Path: "/",
Domain: host,
Expires: time.Now().Add(expireDuration),
Secure: sessionSecure,
SameSite: http.SameSiteLaxMode,
MaxAge: int(expireDuration.Seconds()),
HttpOnly: true,
},
)
} else {
http.SetCookie(w,
&http.Cookie{
Name: key,
Value: "_",
Path: "/",
Domain: host,
Expires: oldTime,
Secure: sessionSecure,
SameSite: http.SameSiteLaxMode,
MaxAge: -1,
HttpOnly: true,
},
)
}
}
}
// GetCookieValue is the simple cookie getter.
func GetCookieValue(req *http.Request, name string) (cookieValue string) {
if cookie, _ := req.Cookie(name); cookie != nil {
cookieValue = cookie.Value
}
return
}