Auth with passport.js

Mmagicspon11/12/2022
Evening all,

Has anyone successfully setup trpc with passport.js for auth?
UUUnknown User12/16/2022
Message Not Public
Sign In & Join Server To View
Mmsalsbery12/16/2022
What particular issues using passport auth with tRPC? While passport auth may not fit with tRPC APIs, it can certainly be used with tRPC. With tRPC middleware, context, and custom links one has pretty good access to the request/response pipeline when using the Express adapter, so it's pretty flexible
UUUnknown User1/14/2023
Message Not Public
Sign In & Join Server To View
Nnlucas1/14/2023
tRPC expects a middleware to return opts.next(opts)

I haven't used passport, but if passport returns the result of the next which you're passing to it, you can just add a return, and then might need to pass something like () => opts.next(opts) to it
Nnlucas1/14/2023
The docs are worth a read: https://trpc.io/docs/middlewares
Mmsalsbery1/14/2023
@NT I don’t think you can use Express (connect) middleware directly as tRPC middleware (I could be wrong). I would put the Passport middleware in an Express router before the tRPC adapter
Nnlucas1/14/2023
That's also a good point if it's designed for Express. Probably there are some lower level APIs you could use to build a tRPC middleware via Passport, or someone has published one already. I wouldn't hoist the middleware up out of tRPC in most cases though; you might want to vary how auth is handled based on the procedure in the future
Mmsalsbery1/14/2023
For me I use a hybrid of Express and tRPC routers. Super flexible, and for Passport, it depends on which authentication strategy I’m using. For basic authentication and tokens I just roll my own. For more complex strategies I’ll hoist Passport to Express and expose whatever I need in tRPC context/middleware