Example #1
0
// Auth Handler for net/http
func (a *AuthRoute) AuthHandler(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		userId, token, err := a.authenticate(w, r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusUnauthorized)
			return
		}
		context.Set(r, TokenKey, token)
		context.Set(r, UserKey, userId)
		h.ServeHTTP(w, r)
		context.Clear(r)
	})
}
Example #2
0
// Auth middleware for negroni
func (a *AuthRoute) AuthMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	userId, token, err := a.authenticate(w, r)

	if err != nil {
		http.Error(w, err.Error(), http.StatusUnauthorized)
		return
	}

	context.Set(r, TokenKey, token)
	context.Set(r, UserKey, userId)
	next(w, r)
	context.Clear(r)

}
Example #3
0
func setCurrentRoute(r *http.Request, val interface{}) {
	context.Set(r, routeKey, val)
}
Example #4
0
func setVars(r *http.Request, val interface{}) {
	context.Set(r, varsKey, val)
}