sonr/internal/session/cookies.go
Prad Nukala edb109b542
feature/dwn sw js (#1103)
- **feat(macaroon): add  and  to macaroon genesis**
- **refactor: move schema definitions to dedicated file**
- **feat: remove Session model**
- **refactor: move session middleware to internal package**
2024-10-02 01:40:49 -04:00

42 lines
667 B
Go

package session
import (
"net/http"
"time"
"github.com/donseba/go-htmx"
"github.com/labstack/echo/v4"
)
type Session struct {
echo.Context
htmx *htmx.HTMX
}
func (c *Session) Htmx() *htmx.HTMX {
return c.htmx
}
func (c *Session) ID() string {
return ReadCookie(c, "session")
}
func ReadCookie(c echo.Context, key string) string {
cookie, err := c.Cookie(key)
if err != nil {
return ""
}
if cookie == nil {
return ""
}
return cookie.Value
}
func WriteCookie(c echo.Context, key string, value string) {
cookie := new(http.Cookie)
cookie.Name = key
cookie.Value = value
cookie.Expires = time.Now().Add(24 * time.Hour)
c.SetCookie(cookie)
}