2015-02-20 04:03:22 -08:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-02-20 05:08:11 -08:00
|
|
|
"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 {
|
2015-02-20 05:08:11 -08:00
|
|
|
return authboss.RenderErr{tpl.Name(), data, views.ErrTemplateNotFound}
|
2015-02-20 04:03:22 -08:00
|
|
|
}
|
|
|
|
|
2015-02-20 05:08:11 -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)
|
2015-02-20 05:08:11 -08:00
|
|
|
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)
|
2015-02-20 05:08:11 -08:00
|
|
|
data.MergeKV(authboss.FlashErrorKey, flash)
|
2015-02-20 04:03:22 -08:00
|
|
|
}
|
|
|
|
|
2015-02-20 05:08:11 -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}
|
|
|
|
}
|
|
|
|
|
2015-02-20 05:08:11 -08:00
|
|
|
_, err = io.Copy(w, buffer)
|
2015-02-20 04:03:22 -08:00
|
|
|
if err != nil {
|
|
|
|
return authboss.RenderErr{tpl.Name(), data, err}
|
|
|
|
}
|
2015-02-20 05:08:11 -08:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|