1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package middleware
import (
"context"
"encoding/gob"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
)
func init() {
gob.Register(&State{})
}
func PopulateState() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
sess, err := session.Get("state", c)
if err != nil {
return err
}
u := sess.Values["state"]
c.Set("state", u)
ctx := context.WithValue(c.Request().Context(), ContextKey("user"), u)
r := c.Request().WithContext(ctx)
c.SetRequest(r)
return next(c)
}
}
}
func SetState(c echo.Context, u State, maxage int) error {
sess, err := session.Get("state", c)
if err != nil {
return err
}
sess.Values["state"] = u
sess.Options = &sessions.Options{
Path: "/",
MaxAge: maxage,
HttpOnly: true,
Secure: true,
}
return sess.Save(c.Request(), c.Response())
}
type State struct {
Username string `json:"username"`
CurrentGUID string `json:"current_guid"`
Points int `json:"points"`
Image string `json:"image"`
}
|