how would i use ring middleware with a yada resource?
i can't work out how to get a ring handler
the manual (https://juxt.pro/yada/manual/index.html#_resources_as_ring_handlers) says I can use yada/handler
to get a ring handler, but it returns me a yada.handler.Handler
rather than a function which takes the request and returns the response
Actually you can treat that record as a fn since it satisfies IFn.
But, yada handlers are not Ring handlers as they return async values since yada is async
They are aleph handlers I suppose?
I've never had ring middleware that I needed with yada, @conan what's the use-case?
i want to redirect users off-site to perform auth
In yada you would solve that differently. You would attach a 401 handler to your resources using either a custom resource function or a postwalk over your resources.
ideally i'd wrap collections of routes in some middleware, but my understanding is that yada prefers this to be pushed down into the handlers, so i'm trying to wrap my handlers in something that will bypass the handler function and return a 307 in certain circumstances
i don't want to return 401 in these instances, the aim is to redirect the user
Yes, push down to each handler. The idea is that web resources are testable independent of routing
Correct. A 401 handler is run when a 401 has been produced. You get the option to produce a new result.
right but i don't want to produce 401 here
that'll come later. after completing authentication, we'll issue the user with a cookie that will provide them access to our API resources; in the case that this cookie is not valid for some reason, we'll return a 401. there's no point doing that when they don't have a cookie; it would be possible to serve the client-side SPA and have it handle the 401, but that would make everything painfully slow
the most common path will be no-cookie > auth > cookie > API, and that's what i'm aiming to optimise for
The lack of cookie will trigger a 401 from your authenticator.
yeah i can set up the yada-security/verify
function to do that, but I don't want to send a 401 when there's no cookie - in fact, I don't even want to do any routing when there's no cookie. if you don't have a cookie, you're on the wrong site
is there a way to intercept the request? perhaps that's a bidi question rather than a yada one?
Yes, you can add an interceptor. You should intercept resources not routing.
OK, so I think i'm on the right track, i'm writing a function to wrap a resource, but what i don't know is how i can turn a yada resource into something i can use with ring middelware - if i wrap any resource in middleware it seems to give no response
(that includes wrapping the resource in yada/handler
first)
Have you tried deref on the response? You'd need to wrap the handler in a wrapper that would deref the response. But that would break the async model so there are performance tradeoffs
is there a way to bypass that?
i don't need to create futures and things, they will never be used in this scenario
there won't be a performance tradeoff here as we don't need to do anything except send a redirect, i don't think that can be made any faster
Fwiw, I'm not recommending you produce a 401 to the end user, I'm saying that yada let's you override the behaviour of the 401 and convert it to a 301
oh you mean change the 401 response after it's been produced?
that seems a bit hacky, is there a recommended approach to changing the response?
The yada author does it in his software 😛 , not hacky afaik
ok i'll give it a shot, thanks
That rewrite behaviour is in the manual, one sec
Status responses