@@ -18,33 +18,19 @@ type SessionsStore struct {
18
18
19
19
// Get should return a cached session.
20
20
func (st * SessionsStore ) Get (r * http.Request , name string ) (* sessions.Session , error ) {
21
- chiStore := chiSession .GetSession (r )
22
-
23
- rawData := chiStore .Get (name )
24
- if rawData == nil {
25
- return st .New (r , name )
26
- }
27
-
28
- oldSession , ok := rawData .(* sessions.Session )
29
- if ! ok {
30
- return nil , fmt .Errorf ("unexpected object in session: %v at name: %s" , rawData , name )
31
- }
32
-
33
- // Copy over the old data into the session
34
- session := sessions .NewSession (st , name )
35
- session .ID = oldSession .ID
36
- session .IsNew = oldSession .IsNew
37
- session .Options = oldSession .Options
38
- session .Values = oldSession .Values
39
-
40
- return session , nil
21
+ return st .getOrNew (r , name , false )
41
22
}
42
23
43
24
// New should create and return a new session.
44
25
//
45
26
// Note that New should never return a nil session, even in the case of
46
27
// an error if using the Registry infrastructure to cache the session.
47
28
func (st * SessionsStore ) New (r * http.Request , name string ) (* sessions.Session , error ) {
29
+ return st .getOrNew (r , name , true )
30
+ }
31
+
32
+ // getOrNew gets the session from the chi-session if it exists. Override permits the overriding of an unexpected object.
33
+ func (st * SessionsStore ) getOrNew (r * http.Request , name string , override bool ) (* sessions.Session , error ) {
48
34
chiStore := chiSession .GetSession (r )
49
35
50
36
session := sessions .NewSession (st , name )
@@ -60,6 +46,8 @@ func (st *SessionsStore) New(r *http.Request, name string) (*sessions.Session, e
60
46
session .Values = oldSession .Values
61
47
62
48
return session , nil
49
+ } else if ! override {
50
+ return nil , fmt .Errorf ("unexpected object in session: %v at name: %s" , rawData , name )
63
51
}
64
52
}
65
53
0 commit comments