Can't access certain session data with Gin sessions?

Viewed 114

I'm new to Gin and Gin sessions, and I have a weird problem I cannot explain: It seems that session data I write in my controller can't be accessed from my middlware.

Allow me to demonstrate:

I do the following in my controller:

func OidcCallBack(c *gin.Context) {
...
    // store user info and redirect to original location
    userJsonData, err := json.Marshal(*user)
    if err != nil {
        log.Errorf("error encoding user data: %s", err.Error())
    }
    session.Set(SessionUserInfo, string(userJsonData))
    session.Save()

}

In my middleware, I want to read this session data:

func OidcMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        // get our session variables
        session := sessions.Default(c)
        // some test code, this works btw
        session.Set("bla", "test")
        session.Save()
        blavar := session.Get("bla")
        // get our userinfo
        userinfo := session.Get(controllers.SessionUserInfo) // this results in nil
        log.Debug(blavar)
        if userinfo == nil {
            log.Debugf("filter incoming url: %s", c.Request.URL.String())
            session.Set(controllers.SessionRedirectTarget, c.Request.URL.String())
            session.Save()
            c.Redirect(http.StatusFound, controllers.AuthLogin)
            c.Abort()
        }
        c.Next()
    }
}

When I look in my debugger, I notice that the data set in the controller, ended up in a different place of session.store.MemStore.cache.data:

session data set in the middleware ends up in data[0], and the data set in the controller ends up in data1.

debugger output

Can someone explain me what I need to do to make sure all data ends up in the same spot, and also explain me why this happens? I can't find in any info on this in the readme file.

0 Answers
Related