T
Join ServertRPC
❓-help
Auth with passport.js
Evening all,
Has anyone successfully setup trpc with passport.js for auth?
Has anyone successfully setup trpc with passport.js for auth?
Message Not Public
Sign In & Join Server To View
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
Message Not Public
Sign In & Join Server To View
tRPC expects a middleware to
I haven't used passport, but if passport returns the result of the
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 itThe docs are worth a read: https://trpc.io/docs/middlewares
@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
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
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