feat: add session TTL management for user sessions

This commit is contained in:
2025-05-30 17:33:35 -04:00
parent a120c37a46
commit 471c968c3f
3 changed files with 79 additions and 1 deletions

View File

@@ -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())
}

View File

@@ -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
}

View File

@@ -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"