37 lines
728 B
Go
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
|
|
}
|
|
}
|
|
}
|