From e83c830d057ab0535d0ba173d9a35a685d62a794 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Mon, 23 Nov 2020 09:28:43 +0800 Subject: update --- store.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/store.go b/store.go index b730903..a17fda6 100644 --- a/store.go +++ b/store.go @@ -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. -- cgit v1.2.3