diff options
author | Hank Shen <swhbox@foxmail.com> | 2020-11-23 04:28:43 +0300 |
---|---|---|
committer | Hank Shen <swhbox@foxmail.com> | 2020-11-23 04:28:43 +0300 |
commit | e83c830d057ab0535d0ba173d9a35a685d62a794 (patch) | |
tree | 1c15e2047574938baad6407aa6dae3826189d461 | |
parent | d1d15b8bee221bb4a393bf2ca565777036b61121 (diff) |
update
-rw-r--r-- | store.go | 25 |
1 files changed, 23 insertions, 2 deletions
@@ -34,6 +34,11 @@ type Store interface { Save(ctx echo.Context, s *Session) error } +// IDGenerator session id generator +type IDGenerator interface { + GenerateID(ctx echo.Context, session *Session) (string, error) +} + // CookieStore ---------------------------------------------------------------- // NewCookieStore returns a new CookieStore. @@ -90,13 +95,29 @@ func (s *CookieStore) New(ctx echo.Context, name string) (*Session, error) { s.Codecs...) if err == nil { session.IsNew = false + session.ID = v } } return session, err } -func (s *CookieStore) Reload(_ echo.Context, _ *Session) error { - return nil +func (s *CookieStore) Reload(ctx echo.Context, session *Session) error { + if len(session.ID) == 0 { + return nil + } + err := securecookie.DecodeMultiWithMaxAge( + session.Name(), session.ID, &session.Values, + ctx.CookieOptions().MaxAge, + s.Codecs...) + if err == nil { + session.IsNew = false + } + return err +} + +func (s *CookieStore) GenerateID(ctx echo.Context, session *Session) (string, error) { + return securecookie.EncodeMulti(session.Name(), session.Values, + s.Codecs...) } // Save adds a single session to the response. |