Add IsNew flag on sessions

Former-commit-id: 94ac010a156bbe124033da2cbaac05fc4726d189
This commit is contained in:
corebreaker 2017-08-01 11:00:30 +03:00
parent ef979cf541
commit 9b61ce2531
2 changed files with 15 additions and 2 deletions

View File

@ -17,6 +17,7 @@ type (
// This is what will be returned when sess := sessions.Start().
Session struct {
sid string
isNew bool
values memstore.Store // here are the real values
// we could set the flash messages inside values but this will bring us more problems
// because of session databases and because of
@ -26,7 +27,6 @@ type (
// NOTE: flashes are not managed by third-party, only inside session struct.
flashes map[string]*flashMessage
mu sync.RWMutex
createdAt time.Time
expireAt *time.Time // nil pointer means no expire date
timer *time.Timer
provider *provider
@ -44,6 +44,11 @@ func (s *Session) ID() string {
return s.sid
}
// IsNew returns true if is's a new session
func (s *Session) IsNew() bool {
return s.isNew
}
// HasExpireDate test if this session has an expire date, if not, this session never expires
func (s *Session) HasExpireDate() bool {
return s.expireAt != nil
@ -285,6 +290,7 @@ func (s *Session) set(key string, value interface{}, immutable bool) {
s.mu.Unlock()
s.updateDatabases()
s.isNew = false
}
// Set fills the session with an entry"value", based on its "key".
@ -336,6 +342,10 @@ func (s *Session) Delete(key string) bool {
s.mu.Unlock()
s.updateDatabases()
if removed {
s.isNew = false
}
return removed
}
@ -357,6 +367,7 @@ func (s *Session) Clear() {
s.mu.Unlock()
s.updateDatabases()
s.isNew = false
}
// ClearFlashes removes all flash messages.

View File

@ -107,6 +107,8 @@ func (s *Sessions) Start(ctx context.Context) *Session {
sid := s.config.SessionIDGenerator()
sess := s.provider.Init(sid, s.config.Expires)
sess.isNew = len(sess.values) == 0
s.updateCookie(sid, ctx, s.config.Expires)
return sess
@ -114,8 +116,8 @@ func (s *Sessions) Start(ctx context.Context) *Session {
cookieValue = s.decodeCookieValue(cookieValue)
sess := s.provider.Read(cookieValue, s.config.Expires)
return sess
return sess
}
// ShiftExpiraton move the expire date of a session to a new date by using session default timeout configuration