Skip to content

Middleware next? #564

Closed
Closed

Description

@delaneyj

Unless I'm missing something you can't have a middleware do pre/post style flow ala koa...

.middleware(async ({ ctx, next }) => {
  const auth = checkForAuth(ctx)
  if (!auth) return handleUnauth(ctx)
  await next()
  const update = checkForUpdatedAuth(ctx)
  if (update) {
    ctx.res.headers['x-access-token'] = update.token
  }
})

Activity

  1. KATT commented on Jun 19, 2021

    @KATT
    Member

    You are correct.

    I actually considered calling middlewares something else when I made them as it is not the same as express middlewares. Originally it was called liked preHook or something like that, but ended up being called `middleware. See the original PR #87

    • What would you want to use next()-calling for? Is there a need for a connect-like middleware layer in tRPC?
    • I agree middleware isn't the perfect word for this implementation - I'd be up for changing it - suggestions?
  2. delaneyj commented on Jun 19, 2021

    @delaneyj
    Author

    Well in my exact situation I'm parsing a paseto token in the preNext, and in the postNext looking at the expiration date and if its within a bound using the refreshToken to regen the accessToken and send it back in headers. While you could do this outside of tRPC, you can't if you want to be protocol agnostic. You could do a pre/post hook, but as Koa has shown, just having async/await cleans thing up and keep stuff in logical places.

  3. KATT commented on Jun 20, 2021

    @KATT
    Member

    I'd do both of those things in createContext (and attach the user obj to ctx so it's passed through to all resolvers).

    Doesn't seem like you need do that token regen after resolving the request, or am I misunderstanding?

    Secondly, createContext is called only once per request even if you're batching, but mutating as part of a middleware will be multiplied with the number of calls you did in that req and might produce some odd side effects.

  4. delaneyj commented on Jun 20, 2021

    @delaneyj
    Author

    right now your encode/decode goes through transformer. but it could be a generic middleware if it was more like koa. also supporting things like brotli over the wire would just be another middleware.

  5. KATT commented on Jun 20, 2021

    @KATT
    Member

    The goal wasn't really for tRPC to be completely standalone, but something you can plug in to whatever server you used. I first created the createHttpServer for the integration testing, exposing it was just a "meh, why not?" side-effect.

    Anyway, middlewares on a router level will not be the solution for your auth as they can be called several times in the same request (batching). Do this stuff in a connect middleware outside of tRPC or in your createContext.

    I won't change the current implementation - but I understand the naming is confusing, so open for suggestions on that.

  6. locked and limited conversation to collaborators on Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions