refactor: rename internal/session package to internal/ctx

This commit is contained in:
Prad Nukala
2024-10-08 17:22:14 -04:00
parent 35348fef6a
commit 25cddbada6
11 changed files with 10 additions and 12 deletions
+33
View File
@@ -0,0 +1,33 @@
package ctx
import (
"github.com/labstack/echo/v4"
"github.com/segmentio/ksuid"
)
// GetSession returns the current Session
func GetSession(c echo.Context) *Session {
return c.(*Session)
}
// UseSession establishes a Session Cookie.
func UseSession(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
sc := initSession(c)
headers := new(RequestHeaders)
err := sc.Bind(headers)
if err != nil {
return err
}
return next(sc)
}
}
func initSession(c echo.Context) *Session {
s := &Session{Context: c}
if val := ReadCookie(c, "session"); val == "" {
id := ksuid.New().String()
WriteCookie(c, "session", id)
}
return s
}