fixed new logic
This commit is contained in:
		
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							| @@ -3,6 +3,7 @@ module github.com/dalu/mongostore | ||||
| go 1.14 | ||||
|  | ||||
| require ( | ||||
| 	github.com/davecgh/go-spew v1.1.1 | ||||
| 	github.com/gorilla/securecookie v1.1.1 | ||||
| 	github.com/gorilla/sessions v1.2.0 | ||||
| 	go.mongodb.org/mongo-driver v1.3.1 | ||||
|   | ||||
							
								
								
									
										25
									
								
								store.go
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								store.go
									
									
									
									
									
								
							| @@ -50,28 +50,19 @@ func (s *MongoStore) MaxAge(age int) { | ||||
|  | ||||
| func (s *MongoStore) New(r *http.Request, name string) (*sessions.Session, error) { | ||||
| 	session := sessions.NewSession(s, name) | ||||
| 	session.Options = &sessions.Options{ | ||||
| 		Path:     s.Options.Path, | ||||
| 		MaxAge:   s.Options.MaxAge, | ||||
| 		Domain:   s.Options.Domain, | ||||
| 		Secure:   s.Options.Secure, | ||||
| 		HttpOnly: s.Options.HttpOnly, | ||||
| 	} | ||||
| 	opts := *s.Options | ||||
| 	session.Options = &opts | ||||
| 	session.IsNew = true | ||||
| 	cookie, e := r.Cookie(name) | ||||
| 	if e != nil { | ||||
| 		return session, e | ||||
| 	} | ||||
| 	if e == nil { | ||||
| 		e = securecookie.DecodeMulti(name, cookie.Value, &session.ID, s.Codecs...) | ||||
| 	if e != nil { | ||||
| 		return session, e | ||||
| 	} | ||||
| 		if e == nil { | ||||
| 			e = s.load(session) | ||||
| 	if e != nil { | ||||
| 		return session, e | ||||
| 	} else { | ||||
| 			if e == nil { | ||||
| 				session.IsNew = false | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	return session, nil | ||||
| } | ||||
|  | ||||
| @@ -80,7 +71,7 @@ func (s *MongoStore) Get(r *http.Request, name string) (*sessions.Session, error | ||||
| } | ||||
|  | ||||
| func (s *MongoStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error { | ||||
| 	if session.Options.MaxAge < 0 { | ||||
| 	if session.Options.MaxAge <= 0 { | ||||
| 		if e := s.remove(session); e != nil { | ||||
| 			return e | ||||
| 		} | ||||
|   | ||||
| @@ -44,14 +44,14 @@ func TestFlashes(t *testing.T) { | ||||
| 	var session *sessions.Session | ||||
| 	var flashes []interface{} | ||||
|  | ||||
| 	ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) | ||||
| 	ctx := context.Background() | ||||
| 	client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017")) | ||||
| 	err = client.Ping(ctx, readpref.Primary()) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
|  | ||||
| 	store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second) | ||||
| 	store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second, []byte("hellol-worlol")) | ||||
| 	store.Options.Path = "/" | ||||
| 	store.Options.MaxAge = 86400 * 30 * 365 | ||||
| 	defer store.Close() | ||||
| @@ -199,42 +199,6 @@ func TestFlashes(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestCookieStoreMapPanic(t *testing.T) { | ||||
| 	defer func() { | ||||
| 		err := recover() | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) | ||||
| 	client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017")) | ||||
| 	err = client.Ping(ctx, readpref.Primary()) | ||||
| 	if err != nil { | ||||
| 		panic(err) | ||||
| 	} | ||||
|  | ||||
| 	store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second, []byte("aaa0defe5d2839cbc46fc4f080cd7adc")) | ||||
| 	store.Options.Path = "/" | ||||
| 	store.Options.MaxAge = 86400 * 30 * 365 | ||||
| 	defer store.Close() | ||||
|  | ||||
| 	req, err := http.NewRequest("GET", "http://www.example.com", nil) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("failed to create request", err) | ||||
| 	} | ||||
| 	w := httptest.NewRecorder() | ||||
|  | ||||
| 	session := sessions.NewSession(store, "hello") | ||||
|  | ||||
| 	session.Values["data"] = "hello-world" | ||||
|  | ||||
| 	err = session.Save(req, w) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("failed to save session", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func init() { | ||||
| 	gob.Register(FlashMessage{}) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Darko Luketic
					Darko Luketic