-
Notifications
You must be signed in to change notification settings - Fork 7
Description
I was adding an authentication check, and thought that it would be nice if it were part of the routing (at least in the application I was dealing with, I didn't want to even reveal the existence of the paths to non authenticated users). The combinators like segment
, end
, etc can't access the application context (which I wonder if they should be able to!), but I was able to make something that I could use like:
segment // path "blah" // myauth ctxt // ... ==> ...
And it was pretty easy to do, but mainly because I already had an idea how the rest of the combinators worked, as I was able to just modify anything
to get something like:
myauth :: Ctxt -> Req -> IO (Maybe (Req, a -> a))
myauth ctxt req = return $ if authcheck ctxt then Just (req, id) else Nothing
As I wasn't actually trying to do anything interesting with the actual request -- just stop this route if the user wasn't authenticated.
Which made me wonder -- would it be helpful to have a helper to make this type of function (and maybe some others that are similar)? It's not that it's much code, it's just that having a function that says this lets you create a new, application specific routing combinator, might be helpful. For it to be able to modify the path gets tricky, but if it's just reading the request (or as in this case, not even doing that), the interface might not actually be that complicated.