Exemplo n.º 1
0
// Remember the user session by saving to the session cookie.
func Remember(w http.ResponseWriter, r *http.Request, user *models.User) error {
	err := user.Remember()
	if err != nil {
		return err
	}

	// NOTE: do not error here - invalid request cookies will be reset below
	session, err := models.Store.Get(r, models.SessionName)
	if err != nil {
		log.Println(err)
	}

	session.Values["ID"] = user.ID
	session.Values["RememberToken"] = user.RememberToken

	if err := session.Save(r, w); err != nil {
		return err
	}

	err = models.DB.Update(user)
	if err != nil {
		return err
	}

	return nil
}
Exemplo n.º 2
0
func TestUser_Remember(t *testing.T) {
	user := new(models.User)

	err := user.Remember()
	if err != nil {
		t.Fatal(err)
	}

	It("should have a 44 byte token", func() {
		assert.Len(t, user.RememberToken, 44)
	})

	It("should have a valid digest", func() {
		assert.True(t, user.Authenticate())
	})
}