actionhero / ah-next-plugin

For booting a Next.JS React application inside of Actionhero
5 stars 4 forks source link

The next render action seems to be ignoreing middleware #1

Closed ryangall7 closed 4 years ago

ryangall7 commented 4 years ago

I'm trying to add authentication middleware to the a project using the next plugin. But if I set the connection.rawConnection.responseHttpCode in the middleware it's overwritten in the next:render action. It makes sence since only the res and req are getting passed to the next handler.

I don't know if there is way to stop action hero from processing the action in my middleware? or if there is something directly on the res i should be changing to let next handle these correctly.

Basically I want to be able to force a redirect in my middleware:

              connection.rawConnection.responseHeaders.push(['Location', installUrl]);
              connection.rawConnection.responseHttpCode = 302;
evantahler commented 4 years ago

Hmm.... I think there’s a difference of philosophy between actionhero and next here that might not be compatible.

Next wants all users to get the same “Static” page content and then use APIs to hydrate the pages. This means that visitors can always see a page, but then might have empty content if they aren’t authorized, etc.

Can you share more about the use case when you wouldn’t want the “container” page sent to the client? Maybe we can come up with a work-around

ryangall7 commented 4 years ago

That makes sence.

I'm building a Shopify Embedded App which authenticates using OAuth I'm using middleware retrieve a shopOrigin and accessToken stored for each session. In cases where an access token has not been retrieved yet (first request of session) or it has expired, I need to redirect the user through the Auth flow (separate actions that don't invoke the middleware) to retrieved and store the shop credentials.

My method is based on koa-shopify-auth which uses ctx.redirect(authRoute) to send the user down this path before rendering the page.

I sounds like from your answer that maybe I should be handling this redirect on the front end? Maybe exposing a separate action to check the authentication status, and return a authRoute if necessary?

locally I just wrapped the next:render action in a data.connection.rawConnection.responseHttpCode == 200 check which works for me because it cancels the next rendering if the middleware changes the response code to anything but 200. But I would think this would cause all sorts of issues for other use cases?

Thanks! -Ryan

evantahler commented 4 years ago

Ah! That makes sense! Please feel free to send in a Pull Request with that responseHttpCode check! I think that could be generally useful for lots of middleware