feat: add cookie-based session support using alexedwards/scs (#1)
- Add Session wrapper with typed getters (GetString, GetInt, GetBool, etc.) - Add flash message support via Pop methods (PopString, PopInt, etc.) - Add session utilities: Exists, Keys, ID, Clear, Destroy, RenewToken - Create default session manager in New() for zero-config usage - Allow custom session manager via Options.SessionManager - Wrap mux with scs LoadAndSave middleware in Start() - Add session example demonstrating login/logout with flash messages
This commit is contained in:
12
context.go
12
context.go
@@ -2,6 +2,7 @@ package via
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -29,6 +30,7 @@ type Context struct {
|
||||
signals *sync.Map
|
||||
mu sync.RWMutex
|
||||
ctxDisposedChan chan struct{}
|
||||
reqCtx context.Context
|
||||
}
|
||||
|
||||
// View defines the UI rendered by this context.
|
||||
@@ -360,6 +362,16 @@ func (c *Context) GetPathParam(param string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Session returns the session for this context.
|
||||
// Session data persists across page views for the same browser.
|
||||
// Returns a no-op session if no SessionManager is configured.
|
||||
func (c *Context) Session() *Session {
|
||||
return &Session{
|
||||
ctx: c.reqCtx,
|
||||
manager: c.app.sessionManager,
|
||||
}
|
||||
}
|
||||
|
||||
func newContext(id string, route string, v *V) *Context {
|
||||
if v == nil {
|
||||
log.Fatal("create context failed: app pointer is nil")
|
||||
|
||||
Reference in New Issue
Block a user