go-aah / aah

A secure, flexible, rapid Go web framework
https://aahframework.org
MIT License
690 stars 33 forks source link

Flash messages are not deleted after read #265

Open vcraescu opened 4 years ago

vcraescu commented 4 years ago

What version of aah are you using (aah --version)?

aah v0.12.2 cli v0.13.4 go v1.13.7

Does this issue reproduce with the latest release?

Yes.

What operating system are you using (such as macOS, Linux and Windows)?

macOS, Linux

What did you do?

In controller:

c.Session().SetFlash("error", "Some error message")

In view:

{{ flash . "error" }}

What did you expect to see?

I expect to get the flash message only once but when I refresh the page the flash message is still present in the session.

What did you see instead?

The flash message should be removed from the session if it is read once. It is removed from memory but it is not removed from file session.

vcraescu commented 4 years ago

https://github.com/go-aah/aah/blob/3374b69984a054a3f83776851e8c09aa7b9e76a9/http_engine.go#L314

// Set Cookies
    ctx.writeCookies()

    if re.redirect { // handle redirects
        ctx.Log().Debugf("Redirecting to '%s' with status '%d'", re.path, re.Code)
        http.Redirect(ctx.Res, ctx.Req.Unwrap(), re.path, re.Code)
        return
    }

    // Check ContentType and detect it if need be
    if len(re.ContType) == 0 {
        if _, ok := re.Rdr.(*binaryRender); !ok {
            re.ContentType(ctx.detectContentType())
        }
    }
    if len(re.ContType) > 0 {
        ctx.Res.Header().Set(ahttp.HeaderContentType, re.ContType)
    }

    // 'OnHeaderReply' HTTP event
    e.publishOnHeaderReplyEvent(ctx.Res.Header())

    if bodyAllowedForStatus(re.Code) {
        if e.a.viewMgr != nil && re.isHTML() {
            e.a.viewMgr.resolve(ctx)
        }

        e.writeOnWire(ctx)

Apparently the session is saved before template is actually rendered. That's why the flash message is retained.

vcraescu commented 4 years ago

Managed to mitigate this issue using a PostReply subscriber:

    aah.App().HTTPEngine().OnPostReply(func(e *aah.Event) {
        ctx, ok := e.Data.(*aah.Context)
        if !ok {
            return
        }

        if err := aah.App().SessionManager().SaveSession(ctx.Res, ctx.Session()); err != nil {
            ctx.Log().Errorf("error saving session: %v", err)
            return
        }

        ctx.Log().Println("session successfully saved")
    })
jeevatkm commented 4 years ago

@vcraescu Thanks for reporting an issue. I will have a look and address the issue.

vcraescu commented 4 years ago

thanks!

vcraescu commented 4 years ago

@jeevatkm any news?