keeping track of my children's money since 2013
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
728B

  1. package allowances
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. )
  7. const sessionName = "allowances"
  8. type authed func(w http.ResponseWriter, r *http.Request, uid string) error
  9. func (a *Allowances) protected(handler authed) http.HandlerFunc {
  10. return func(w http.ResponseWriter, r *http.Request) {
  11. session, err := a.store.Get(r, sessionName)
  12. if err != nil {
  13. log.Printf("%+v", err)
  14. http.Error(w, err.Error(), http.StatusBadRequest)
  15. return
  16. }
  17. u, ok := session.Values["uuid"]
  18. if !ok {
  19. http.Redirect(
  20. w, r,
  21. prefix["login"],
  22. http.StatusTemporaryRedirect,
  23. )
  24. return
  25. }
  26. err = handler(w, r, u.(string))
  27. if err != nil {
  28. json.NewEncoder(w).Encode(NewFailure(err.Error()))
  29. return
  30. }
  31. }
  32. }