allowances/auth.go
stephen mcquay a6b7c13fdb Major refactor
- use embedded static assets
- added pw management subcommands
2015-08-09 11:52:40 -07:00

37 lines
728 B
Go

package allowances
import (
"encoding/json"
"log"
"net/http"
)
const sessionName = "allowances"
type authed func(w http.ResponseWriter, r *http.Request, uid string) error
func (a *Allowances) protected(handler authed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := a.store.Get(r, sessionName)
if err != nil {
log.Printf("%+v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
u, ok := session.Values["uuid"]
if !ok {
http.Redirect(
w, r,
prefix["login"],
http.StatusTemporaryRedirect,
)
return
}
err = handler(w, r, u.(string))
if err != nil {
json.NewEncoder(w).Encode(NewFailure(err.Error()))
return
}
}
}