You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
The text was updated successfully, but these errors were encountered:
I often write these sorts of things after the ==>, like:
path "admin" ==> adminCheck adminRoutes
adminCheck :: [Route Ctxt] -> Ctxt -> IO (Maybe Response)
adminCheck routes ctxt = do
isAdmin <- checkIfAdmin ctxt
if isAdmin
then route ctxt routes
else notFoundText "Not found"
I'm not sure what a good helper would look like, especially in the part before the ==>. That's partly because I can't sell my pair partners on even Fn's current features like matching on POST params in routes, so I've never considered trying to make anything fancy myself.
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: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: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.
The text was updated successfully, but these errors were encountered: