authboss icon indicating copy to clipboard operation
authboss copied to clipboard

Middleware chaining for authboss route

Open yusufmalikul opened this issue 2 years ago • 1 comments

I have this part on my code:

pub.HandleFunc("/version", logHandler(version))

func logHandler(next http.HandlerFunc) http.HandlerFunc {
    // logger
}

func version(w http.ResponseWriter, r *http.Request) {
    // print version
}

How to call logHandler for authboss route? I take this from authboss-sample but don't know where to apply the middleware chaining:

mux.Mount("/auth", http.StripPrefix("/auth", ab.Config.Core.Router))

or it's not possible without modifying the authboss codebase?

yusufmalikul avatar Jan 06 '22 15:01 yusufmalikul

Try this:

First, change logHandler to this:

func logHandler(next http.handler) http.Handler {
    // logger
}

It is generally better to use http.Handler for middlewares instead of http.HandlerFunc. Handler is an interface which any router satisfies and helps to chain middlewares better.

Then you can wrap the authboss handler like this:

mux.Mount("/auth", http.StripPrefix("/auth", logHandler(ab.Config.Core.Router)))

stephenafamo avatar Jan 23 '22 00:01 stephenafamo