x/payments/middleware.go
Colin Henry 6e60671cfd WIP
2020-05-20 23:18:58 -07:00

28 lines
646 B
Go
Executable File

package payments
import (
"net/http"
"github.com/jchenry/jchenry/auth"
)
func HasTenantAndSubscription(productID string) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
session, err := auth.Store.Get(r, auth.SessionName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if u, ok := session.Values["profile"]; ok {
user := u.(auth.User)
if _, exist := user.Apps[productID]; exist {
next(w, r)
} else {
http.Redirect(w, r, "/subscription", http.StatusSeeOther)
}
}
}
}