Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware chaining for authboss route #330

Open
yusufmalikul opened this issue Jan 6, 2022 · 1 comment
Open

Middleware chaining for authboss route #330

yusufmalikul opened this issue Jan 6, 2022 · 1 comment

Comments

@yusufmalikul
Copy link

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?

@stephenafamo
Copy link
Contributor

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)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants