blue-jay / blueprint

Blueprint for your next web application in Go.
https://blue-jay.github.io/
MIT License
481 stars 77 forks source link

Sessions issue #80

Closed aquint-g closed 4 years ago

aquint-g commented 6 years ago

Hello, i'm trying to make a login / session system on my app but it seems that sessions are flushed when the app is redirecting to another controller.

I re-used the login system provided by within Blueprint. Here is my login controller :

func Signin(w http.ResponseWriter, r *http.Request) {
    c := flight.Context(w, r)
    userLang := c.Param("userLang") // Get lang in url
    // Validate with required fields
    if !c.FormValid("userid", "password") {
        Index(w, r)
        return
    }

    // Form values
    userid := r.FormValue("userid")
    password := r.FormValue("password")

    // Get database result
    result, noRows, err := user.ByUserId(c.DB, userid)

    // Determine if user exists
    if noRows {
        c.FlashWarning("Password is incorrect")
    } else if err != nil {
        // Display error message
        c.FlashErrorGeneric(err)
    } else if (result.Password == password) {
        if (result.Password == "") {
            // User inactive and display inactive message
            c.FlashNotice("Account is inactive so login is disabled.")
        } else {
            // Login successfully
            session.Empty(c.Sess)
            c.Sess.AddFlash(flash.Info{"Login successful!", flash.Success})
            c.Sess.Values["id"] = result.ID
            c.Sess.Values["email"] = result.Email
            c.Sess.Values["userId"] = result.UserId
            c.Sess.Values["sex"] = result.Sex
            c.Sess.Values["group"] = result.Group
            c.Sess.Values["lastLogin"] = result.LastLogin
            c.Sess.Values["vipTime"] = result.VipTime
            c.Sess.Values["lang"] = result.Lang
            c.Sess.Save(r, w)
            fmt.Println(result.Email) // Prints the email correctly

            http.Redirect(w, r, "/"+userLang+"/account", http.StatusFound)
            return
        }
    } else {
        c.FlashWarning("Password is incorrect")
    }

And here is my "account" controller :

func Index(w http.ResponseWriter, r *http.Request) {
    c := flight.Context(w, r)

    v := c.View.New("account/index")
    userLang := c.Param("userLang") // Get lang in url

    translations := lang.GetLang(userLang) //Get lang variables using model /model/lang

    for index, element := range translations {
        v.Vars["lng_"+index] = element
    }

    v.Vars["userId"] = c.Sess.Values["userId"]
    v.Vars["id"] = c.Sess.Values["id"]
    v.Vars["email"] = c.Sess.Values["email"]
    v.Vars["sex"] = c.Sess.Values["sex"]
    v.Vars["group"] = c.Sess.Values["group"]
    v.Vars["lastLogin"] = c.Sess.Values["lastLogin"]
    v.Vars["vipTime"] = c.Sess.Values["vipTime"]
    v.Vars["lang"] = c.Sess.Values["lang"]

    fmt.Println(c.Sess.Values["email"]) // prints "nil"

    v.Render(w, r)
}

I'm working on Windows, and i followed the configuration procedure. By the way, i can't trace any bug on the console..

Thanks for your support !

josephspurrier commented 6 years ago

Can you check the error from c.Sess.Save(r, w) to err = c.Sess.Save(r, w) and output the error to see if it has a problem saving?