magicspon
magicspon3y ago

Auth with passport.js

Evening all,
Has anyone successfully setup trpc with passport.js for auth?
8 Replies
Rammstein
Rammstein2y ago
Any progress regarding this? I am facing a similar challenge
mark salsbery
mark salsbery2y ago
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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Nick
Nick2y ago
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
Nick
Nick2y ago
The docs are worth a read: https://trpc.io/docs/middlewares
Middlewares | tRPC
You are able to add middleware(s) to a procedure with the t.procedure.use() method. The middleware(s) will wrap the invocation of the procedure and must pass through its return value.
mark salsbery
mark salsbery2y ago
@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
Nick
Nick2y ago
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
mark salsbery
mark salsbery2y ago
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