Endgame1013
Endgame1013
TtRPC
Created by Thimo_o on 8/26/2023 in #❓-help
How do I setup App router + TRPC + Lucia Auth? (protected routes)
What I’ve used in a couple projects:
import * as context from 'next/headers';

export const isAuthed = t.middleware(async (opts) => {
const { req } = opts.ctx;
const authRequest = auth.handleRequest(req.method, context);
const session = await authRequest.validate();
if (!session) throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Not authorized!' });
return opts.next({
ctx: {
// 👇 infer session, auth, and authRequest as non-nullable
session,
auth,
authRequest,
},
});
});

export const protectedProcedure = t.procedure.use(loggerMiddleware).use(isAuthed);
import * as context from 'next/headers';

export const isAuthed = t.middleware(async (opts) => {
const { req } = opts.ctx;
const authRequest = auth.handleRequest(req.method, context);
const session = await authRequest.validate();
if (!session) throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Not authorized!' });
return opts.next({
ctx: {
// 👇 infer session, auth, and authRequest as non-nullable
session,
auth,
authRequest,
},
});
});

export const protectedProcedure = t.procedure.use(loggerMiddleware).use(isAuthed);
7 replies
TtRPC
Created by benjaminreid on 11/1/2023 in #❓-help
Would you recommend tRPC’s usage in this case?
No description
7 replies
TtRPC
Created by benjaminreid on 11/1/2023 in #❓-help
Would you recommend tRPC’s usage in this case?
I’ve personally never implemented something like this with tRPC, but it does sound possible. Personally, I’ve found monorepos and shared packages to be a pretty major headache, especially as projects begin to scale (handling different typescript configs, cjs/esm, build steps, etc.). If you want to keep things tightly coupled and are confident you’re gonna be dealing with only typescript clients for the foreseeable future, then I’d stick with tRPC for this for sure. If not, GraphQL would probably fit this quite nicely.
Speaking from experience, tRPC is perfect for BFF (backend for frontend), but trying to extend it out to non-typescript clients (via openapi or something of the like) can really be a pain. GraphQL, on the other hand, solves this problem a bit more elegantly, since it’s mostly language-agnostic. But it does come with its fair share of tradeoffs, too. Pick your poison.
7 replies
TtRPC
Created by ByteBuf on 6/21/2023 in #❓-help
Using tRPC and something else too? For building a mobile app / public API.
I would try using the trpc open api extension first and if that doesn’t fit your needs, I have found GraphQL to work well on mobile.
3 replies