大猩猩的session.AddFlash不会添加闪存消息。

huangapple go评论91阅读模式
英文:

Gorilla session.AddFlash Does Not Add Flash Message

问题

我有一个注册页面,其中包含两个处理程序,一个用于显示表单,一个用于处理表单提交。

我试图使用session.AddFlash方法保存错误,然后进行302重定向回注册表单并显示错误。

我设置了一个session存储:

package web

import (
    "github.com/gorilla/sessions"
)

var sessionStore = sessions.NewCookieStore([]byte(sessionSecret))

然后我的处理程序如下所示:

package web

import (
    "html/template"
    "log"
    "net/http"
)

func registerForm(w http.ResponseWriter, r *http.Request) {
    session, err := sessionStore.Get(r, "mysession")
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    data := map[string]interface{}{}
    log.Print("Flashes: ")
    log.Print(session.Flashes())
    if flashes := session.Flashes(); len(flashes) > 0 {
        data["error"] = flashes[0]
    }

    tmpl, _ := template.ParseFiles("web/templates/register.html.tmpl")
    tmpl.Execute(w, data)
}

func register(w http.ResponseWriter, r *http.Request) {
    session, err := sessionStore.Get(r, "mysession")
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    r.ParseForm()
    username := r.Form["username"][0]
    password := r.Form["password"][0]

    if UserExists(username) {
        log.Print("Username already taken")
        session.AddFlash("Username already taken")
        http.Redirect(w, r, "/web/register", http.StatusFound)
        return
    }

    _, err = CreateUser(username, password)
    log.Print(err)

    if err != nil {
        session.AddFlash(err.Error())
        http.Redirect(w, r, "/web/register", http.StatusFound)
        return
    }

    http.Redirect(w, r, "/web/login", http.StatusFound)
}

通过添加日志,我可以看到UserExists返回true,因此应该添加一个闪存消息,但是在重定向到表单处理程序后,会话中没有保存闪存消息。

英文:

I have a registration page with two handlers, one for displaying the form, one for processing a form submission.

I am trying to use a session.AddFlash method to save an error, then do 302 redirect back to the registration form and display the error.

I set up a session store:

package web

import (
	"github.com/gorilla/sessions"
)

var sessionStore = sessions.NewCookieStore([]byte(sessionSecret))

Then my handlers look like this:

package web

import (
	"html/template"
	"log"
	"net/http"
)

func registerForm(w http.ResponseWriter, r *http.Request) {
	session, err := sessionStore.Get(r, "mysession")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	data := map[string]interface{}{}
	log.Print("Flashes: ")
	log.Print(session.Flashes())
	if flashes := session.Flashes(); len(flashes) > 0 {
		data["error"] = flashes[0]
	}

	tmpl, _ := template.ParseFiles("web/templates/register.html.tmpl")
	tmpl.Execute(w, data)
}

func register(w http.ResponseWriter, r *http.Request) {
	session, err := sessionStore.Get(r, "mysession")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	r.ParseForm()
	username := r.Form["username"][0]
	password := r.Form["password"][0]

	if UserExists(username) {
		log.Print("Username already taken")
		session.AddFlash("Username already taken")
		http.Redirect(w, r, "/web/register", http.StatusFound)
		return
	}

	_, err = CreateUser(username, password)
	log.Print(err)

	if err != nil {
		session.AddFlash(err.Error())
		http.Redirect(w, r, "/web/register", http.StatusFound)
		return
	}

	http.Redirect(w, r, "/web/login", http.StatusFound)
}

By adding logs I can see that UserExists returns true therefor a flash message should be added however after redirection to the form handler there is no flash message saved in the session.

答案1

得分: 4

我认为在重定向之前,你需要保存会话。

session.Save(r, w)

http://www.gorillatoolkit.org/pkg/sessions#Session.Save

英文:

I think you have to save the session before you redirect.

session.Save(r, w)

http://www.gorillatoolkit.org/pkg/sessions#Session.Save

huangapple
  • 本文由 发表于 2015年11月12日 20:49:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/33671814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定