Files
motr/middleware/session/middleware.go

38 lines
678 B
Go

//go:build js && wasm
// +build js,wasm
package session
import (
"github.com/labstack/echo/v4"
)
// Context is a session context
type Context struct {
echo.Context
ID string `json:"id"`
}
// Middleware is a middleware that adds a new key to the context
func Middleware() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
id := DetermineID(c)
ctx := &Context{
Context: c,
ID: id,
}
return next(ctx)
}
}
}
// Unwrap unwraps the session context
func Unwrap(c echo.Context) *Context {
cc := c.(*Context)
if cc == nil {
panic("failed to unwrap session context")
}
return cc
}