livebud / bud

The Full-Stack Web Framework for Go
MIT License
5.58k stars 179 forks source link

Middleware support? #406

Open tadasv opened 1 year ago

tadasv commented 1 year ago

I was trying to find find documentation of how to add custom middlewares? An example would be an auth middleware that checks whether user is logged in or not. How would you achieve that with bud?

matthewmueller commented 1 year ago

Hey @tadasv this will becoming a future release. For now, you can add a function at the top of your controller actions to do these sorts of checks.

Something like:

package users

type Controller {
  // Deps
}

func (c *Controller) Create(w http.ResponseWriter, r *http.Request) {
  if !users.Authenticated(w, r) {
    return http.Error(w, "forbidden", 400)
  } 
}