1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-26 05:27:33 +02:00

59 lines
1.6 KiB
Go
Raw Normal View History

2015-02-20 04:03:22 -08:00
package render
import (
"bytes"
"html/template"
2015-02-20 04:03:22 -08:00
"io"
"net/http"
"gopkg.in/authboss.v0"
"gopkg.in/authboss.v0/internal/views"
)
// View renders a view with xsrf and flash attributes.
func View(ctx *authboss.Context, w http.ResponseWriter, r *http.Request, t views.Templates, name string, data authboss.HTMLData) error {
tpl, ok := t[name]
if !ok {
return authboss.RenderErr{tpl.Name(), data, views.ErrTemplateNotFound}
2015-02-20 04:03:22 -08:00
}
data.MergeKV("xsrfName", template.HTML(authboss.Cfg.XSRFName), "xsrfToken", template.HTML(authboss.Cfg.XSRFMaker(w, r)))
if authboss.Cfg.LayoutDataMaker != nil {
data.Merge(authboss.Cfg.LayoutDataMaker(w, r))
}
2015-02-20 04:03:22 -08:00
if flash, ok := ctx.CookieStorer.Get(authboss.FlashSuccessKey); ok {
ctx.CookieStorer.Del(authboss.FlashSuccessKey)
data.MergeKV(authboss.FlashSuccessKey, flash)
2015-02-20 04:03:22 -08:00
}
if flash, ok := ctx.CookieStorer.Get(authboss.FlashErrorKey); ok {
ctx.CookieStorer.Del(authboss.FlashErrorKey)
data.MergeKV(authboss.FlashErrorKey, flash)
2015-02-20 04:03:22 -08:00
}
buffer := &bytes.Buffer{}
err := tpl.ExecuteTemplate(buffer, tpl.Name(), data)
2015-02-20 04:03:22 -08:00
if err != nil {
return authboss.RenderErr{tpl.Name(), data, err}
}
_, err = io.Copy(w, buffer)
2015-02-20 04:03:22 -08:00
if err != nil {
return authboss.RenderErr{tpl.Name(), data, err}
}
return nil
2015-02-20 04:03:22 -08:00
}
// Redirect sets any flash messages given and redirects the user.
func Redirect(ctx *authboss.Context, w http.ResponseWriter, r *http.Request, path, flashSuccess, flashError string) {
if len(flashSuccess) > 0 {
ctx.CookieStorer.Put(authboss.FlashSuccessKey, flashSuccess)
}
if len(flashError) > 0 {
ctx.CookieStorer.Put(authboss.FlashErrorKey, flashError)
}
http.Redirect(w, r, path, http.StatusTemporaryRedirect)
}