Make Session available in the templates

This commit is contained in:
Noah 2018-04-27 14:49:21 -07:00
parent efeb2e934e
commit a1c84fa1e9

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"time" "time"
gorilla "github.com/gorilla/sessions"
"github.com/kirsle/blog/internal/log" "github.com/kirsle/blog/internal/log"
"github.com/kirsle/blog/internal/middleware" "github.com/kirsle/blog/internal/middleware"
"github.com/kirsle/blog/internal/middleware/auth" "github.com/kirsle/blog/internal/middleware/auth"
@ -35,6 +36,7 @@ type vars struct {
RequestDuration time.Duration RequestDuration time.Duration
// Common template variables. // Common template variables.
Session *gorilla.Session
Message string Message string
Flashes []string Flashes []string
Error error Error error
@ -77,6 +79,7 @@ func Template(w io.Writer, r *http.Request, path string, data interface{}) error
if rw, ok := w.(http.ResponseWriter); ok { if rw, ok := w.(http.ResponseWriter); ok {
rw.Header().Set("Content-Type", "text/html; encoding=UTF-8") rw.Header().Set("Content-Type", "text/html; encoding=UTF-8")
session := sessions.Get(r) session := sessions.Get(r)
v.Session = session
// Flashed messages. // Flashed messages.
if flashes := session.Flashes(); len(flashes) > 0 { if flashes := session.Flashes(); len(flashes) > 0 {