gobuffalo/buffalo

Flash messages are cleared if a session delete is called

Mido-sys opened this issue · 6 comments

Hello,

It seems that flash messages are cleared after a session delete is called. We were able to isolate this issue when form sends a POST request and the page reloads.

c.Flash().Add("errors", "Error")
c.Session().Delete("coupon")

c.Render(http.StatusOk, r.HTML("b.html"))

If we remove the c.Session().Delete("coupon") then the flash messages get displayed on a page redirect. If we refresh the page and call the session delete code then flash messages are displayed.

The current buffalo version is v0.18.7

Could it be related to this #2211 ?

sio4 commented

Hi @Mido-sys, thanks for the report.

I am trying to reproduce the issue with the following steps:

  1. buffalo new app ... && cd app && buffalo pop create
  2. buffalo g resource books title:string && buffalo pop migrate
  3. edit
  4. buffalo dev and try to create a book entry but did not enter the title.

My patch in the step 3 is:

--- a/actions/books.go
+++ b/actions/books.go
@@ -108,11 +108,13 @@ func (v BooksResource) Create(c buffalo.Context) error {
                return err
        }
 
+       c.Session().Set("coupon", "HAPPY_SUMMER")
+       fmt.Println("session", c.Session().Session.Values)
        // Get the DB connection from the context
        tx, ok := c.Value("tx").(*pop.Connection)
        if !ok {
                return fmt.Errorf("no transaction found")
        }
 
        // Validate the data from the html form
        verrs, err := tx.ValidateAndCreate(book)
@@ -129,6 +131,10 @@ func (v BooksResource) Create(c buffalo.Context) error {
                        // correct the input.
                        c.Set("book", book)
 
+                       c.Flash().Add("danger", "Error coupon")
+                       c.Session().Delete("coupon")
+                       fmt.Println("session", c.Session().Session.Values)
+
                        return c.Render(http.StatusUnprocessableEntity, r.HTML("books/new.plush.html"))
                }).Wants("json", func(c buffalo.Context) error {
                        return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs))

So I think the second part of the patch is the same as you quoted:

	c.Flash().Add("danger", "Error coupon")
	c.Session().Delete("coupon")
	fmt.Println("session", c.Session().Session.Values)

	return c.Render(http.StatusUnprocessableEntity, r.HTML("books/new.plush.html"))

But could not reproduce your issue. The only different from your example is the status code returns.

Off topic: Is there any reason that using Flash instead of c.Set("errors", verrs) or along with them? Also response as 200 OK for the error?

sio4 commented

My log for the POST request is:

session map[_flash_:[123 125] authenticity_token:[231 206 10 140 249 119 76 130 239 182 216 165 240 238 88 20 67 41 12 179 40 195 141 90 183 116 67 102 118 14 244 82] coupon:HAPPY_SUMMER requestor_id:b7b5c73d2fe192e7e64c]
session map[_flash_:[123 125] authenticity_token:[231 206 10 140 249 119 76 130 239 182 216 165 240 238 88 20 67 41 12 179 40 195 141 90 183 116 67 102 118 14 244 82] requestor_id:b7b5c73d2fe192e7e64c]
INFO[2022-06-13T19:32:03+09:00] /books/ content_type=application/x-www-form-urlencoded db=0s duration=2.638301ms form="{\"Title\":[\"\"],\"authenticity_token\":[\"wAPNvGiKoo6RcxDQBMesA17vbPWvY3kcPdvTFJOCaPQnzccwkf3uDH7FyHX0KfQXHcZgRoeg9EaKr5By5Yycpg==\"]}" human_size="1.7 kB" method=POST params="{\"Title\":[\"\"],\"authenticity_token\":[\"wAPNvGiKoo6RcxDQBMesA17vbPWvY3kcPdvTFJOCaPQnzccwkf3uDH7FyHX0KfQXHcZgRoeg9EaKr5By5Yycpg==\"]}" path=/books/ render=1.848173ms request_id=b7b5c73d2fe192e7e64c-4a6f47d6e6999939b804 size=1689 status=200

(I tested it with status code 200) and it looks like everything is ok. Could you please add your buffalo info and the whole Create handler? or Could you please test as I did with a vanilla example and share the result and your code?

Hello @sio4 , thanks for checking this. Yes, I will create an example and link the repo here

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

This issue was closed because it has been stalled for 5 days with no activity.