本文整理匯總了Golang中code/google/com/p/gorilla/sessions.Session.Flashes方法的典型用法代碼示例。如果您正苦於以下問題:Golang Session.Flashes方法的具體用法?Golang Session.Flashes怎麽用?Golang Session.Flashes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類code/google/com/p/gorilla/sessions.Session
的用法示例。
在下文中一共展示了Session.Flashes方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Login
func Login(r *http.Request, sess *sessions.Session) (body *shared.Body, tpl *template.Template, redirect string) {
// log.Println("route: login")
var id uint64
if _, ok := sess.Values["id"]; ok {
id = sess.Values["id"].(uint64)
}
if id == 0 {
if r.Method != "POST" {
body, tpl = loginForm(r)
} else {
id, err := authenticate(r)
if err != nil {
body, tpl = loginForm(r)
} else {
sess.Values["id"] = id
fs := sess.Flashes("redirect")
redirect = "/"
if len(fs) > 0 {
redirect = fs[0].(string)
}
}
}
} else {
redirect = "/"
}
return
}
示例2: Logout
func Logout(r *http.Request, sess *sessions.Session) (body *shared.Body, tpl *template.Template, redirect string) {
// log.Println("route: logout")
sess.Values["id"] = uint64(0)
redirect = "/"
fs := sess.Flashes("last")
if len(fs) != 0 {
redirect = fs[0].(string)
}
return
}
示例3: testSessionFlashes
func testSessionFlashes(t *testing.T, store sessions.Store) {
defer context.Close()
var req *http.Request
var rsp *ResponseRecorder
var hdr http.Header
var err error
var ok bool
var cookies []string
var session *sessions.Session
var flashes []interface{}
// Round 1 ----------------------------------------------------------------
req = getRequest()
rsp = NewRecorder()
// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}
// Get a flash.
flashes = session.Flashes()
if len(flashes) != 0 {
t.Errorf("Expected empty flashes; Got %v", flashes)
}
// Add some flashes.
session.AddFlash("foo")
session.AddFlash("bar")
// Custom key.
session.AddFlash("baz", "custom_key")
// Save.
if err = sessions.Save(req, rsp); err != nil {
t.Fatalf("Error saving session: %v", err)
}
hdr = rsp.Header()
cookies, ok = hdr["Set-Cookie"]
if !ok || len(cookies) != 1 {
t.Fatalf("No cookies. Header:", hdr)
}
// Round 2 ----------------------------------------------------------------
req = getRequest()
req.Header.Add("Cookie", cookies[0])
rsp = NewRecorder()
// Get a session.
if session, err = store.Get(req, "session-key"); err != nil {
t.Fatalf("Error getting session: %v", err)
}
// Check all saved values.
flashes = session.Flashes()
if len(flashes) != 2 {
t.Fatalf("Expected flashes; Got %v", flashes)
}
if flashes[0] != "foo" || flashes[1] != "bar" {
t.Errorf("Expected foo,bar; Got %v", flashes)
}
flashes = session.Flashes()
if len(flashes) != 0 {
t.Errorf("Expected dumped flashes; Got %v", flashes)
}
// Custom key.
flashes = session.Flashes("custom_key")
if len(flashes) != 1 {
t.Errorf("Expected flashes; Got %v", flashes)
} else if flashes[0] != "baz" {
t.Errorf("Expected baz; Got %v", flashes)
}
flashes = session.Flashes("custom_key")
if len(flashes) != 0 {
t.Errorf("Expected dumped flashes; Got %v", flashes)
}
}