diff --git a/handlers/page_handler.go b/handlers/page_handler.go index 0d1ff4b..fcd5c8f 100644 --- a/handlers/page_handler.go +++ b/handlers/page_handler.go @@ -1,14 +1,25 @@ +//go:build js && wasm +// +build js,wasm + package handlers import ( + "time" + "github.com/labstack/echo/v4" "github.com/sonr-io/motr/internal/ui/home" "github.com/sonr-io/motr/internal/ui/login" "github.com/sonr-io/motr/internal/ui/register" + "github.com/sonr-io/motr/middleware/kvstore" + "github.com/sonr-io/motr/middleware/session" "github.com/sonr-io/motr/pkg/render" ) func RenderHomePage(c echo.Context) error { + id := session.Unwrap(c).ID + ttl := time.Now().Add(time.Hour * 1) + ttlInt := ttl.Unix() + kvstore.Sessions().SetInt(id, int(ttlInt)) return render.Component(c, home.HomeView()) } diff --git a/middleware/kvstore/store.go b/middleware/kvstore/store.go index aa79c7a..d2979e6 100644 --- a/middleware/kvstore/store.go +++ b/middleware/kvstore/store.go @@ -3,7 +3,12 @@ package kvstore -import "github.com/syumai/workers/cloudflare/kv" +import ( + "encoding/json" + "strconv" + + "github.com/syumai/workers/cloudflare/kv" +) type Store interface { Get(key string) (string, error) @@ -11,6 +16,10 @@ type Store interface { Set(key string, value string) error Delete(key string) error Namespace() *kv.Namespace + GetInt(key string) (int, error) + SetInt(key string, value int) error + GetJSON(key string, v any) error + SetJSON(key string, v any) error } func openStore(name string) Store { @@ -47,3 +56,40 @@ func (s *store) Set(key string, value string) error { func (s *store) Delete(key string) error { return s.namespace.Delete(key) } + +func (s *store) GetInt(key string) (int, error) { + v, err := s.namespace.GetString(key, nil) + if err != nil { + return 0, err + } + i, err := strconv.Atoi(v) + if err != nil { + return 0, err + } + return i, nil +} + +func (s *store) SetInt(key string, value int) error { + return s.namespace.PutString(key, strconv.Itoa(value), nil) +} + +func (s *store) GetJSON(key string, v any) error { + jsonString, err := s.namespace.GetString(key, nil) + if err != nil { + return err + } + err = json.Unmarshal([]byte(jsonString), v) + if err != nil { + return err + } + return nil +} + +func (s *store) SetJSON(key string, v any) error { + jsonString, err := json.Marshal(v) + if err != nil { + return err + } + s.namespace.PutString(key, string(jsonString), nil) + return nil +} diff --git a/pkg/cookies/cookies.go b/pkg/cookies/cookies.go index a131837..85c3393 100644 --- a/pkg/cookies/cookies.go +++ b/pkg/cookies/cookies.go @@ -21,6 +21,27 @@ const ( // SessionRole is the key for the session role cookie. SessionRole CookieKey = "session.role" + // SessionTTL is the key for the session TTL cookie. + SessionTTL CookieKey = "session.ttl" + + // SessionUser is the key for the session user cookie. + SessionUser CookieKey = "session.user" + + // SessionUserHandle is the key for the session user handle cookie. + SessionUserHandle CookieKey = "session.user.handle" + + // SessionVaultAddress is the key for the session vault address cookie. + SessionVaultAddress CookieKey = "session.vault.address" + + // SessionVaultCID is the key for the session vault CID cookie. + SessionVaultCID CookieKey = "session.vault.cid" + + // SessionVaultSchema is the key for the session vault schema cookie. + SessionVaultSchema CookieKey = "session.vault.schema" + + // User is the key for the User cookie. + User CookieKey = "user" + // UserHandle is the key for the User Handle cookie. UserHandle CookieKey = "user.handle"