I have a web server using httprouter and negroni. Users log into this system through external OAuth. We save the token to the encrypted session which indicates whether or not they are logged in. I would like to use a middleware to verify whether or not this token exists, and then kick the user back to the login page if it does not. I want to exclude some routes from using the authentication middleware. There is an example in the negroni README of doing this with gorilla mux, but I can't quite get my head around doing this scalably with httprouter. Something similar to my server setup is below:
router := httprouter.New()
router.GET("/login", Login) // auth not required
router.GET("/", Index) // auth required
s := negroni.Classic()
s.Use(sessions.Sessions("example-web-dev", cookiestore.New([]byte("some garbage"))))
s.Use(authenticator.Get())
s.UseHandler(router)
Where /login
is a route I do not want to require authorization through the middleware and /
is. authenticator.Get()
is my authentication handler func with contents I don't think are relevant to the question.
How can I apply authenticator.Get()
to /
but not /login
? Keeping in mind that there will be several other "public" routes alongside /login
and many other gated routes as well.
Some links: