T3 Next Auth / TRPC Documentation is outdated

I noticed t3 is using next-auth v4 instead of v5 (makes sense, 5 is in beta still). But reading through the docs is still confusing since the links are between v4/v5 and next pages/app router. Does anyone have any reliable sources/examples for a next-auth v5 t3 implementation?
3 Replies
Salumsu
Salumsu5mo ago
I just didn't choose authjs in the cli and followed auth js documentation. Maybe you could do the same?
Hobbs
HobbsOP5mo ago
The next auth docs are wildly different regarding folder structure than t3 app especially between v4 and v5. It seems from the docs t3 is fairly opinionated about the folder structure and how it integrates trpc/middleware so having an idea of how you had linked everything would be helpful since the create t3 app doesn't work out of box with drizzle
Salumsu
Salumsu5mo ago
I just put it inside server/auth just like t3, and Im also using drizzle. I think that you need to add the adapter when you want to use oauth. You can integrate it with trpc middleware by going to /server/api/trpc.ts then import auth from your nextauth.
import { auth } from "../auth";
export const createTRPCContext = async (opts: { headers: Headers }) => {
const session = await auth();

return {
db,
session,
...opts,
};
};
import { auth } from "../auth";
export const createTRPCContext = async (opts: { headers: Headers }) => {
const session = await auth();

return {
db,
session,
...opts,
};
};
just like this, and it will be available on the ctx. here's how you create a protected procedure
export const protectedProcedure = t.procedure
.use(timingMiddleware)
.use(({ ctx, next }) => {
if (!ctx.session?.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

return next({
ctx: {
...ctx,
session: { ...ctx.session, user: ctx.session.user },
},
});
});
export const protectedProcedure = t.procedure
.use(timingMiddleware)
.use(({ ctx, next }) => {
if (!ctx.session?.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

return next({
ctx: {
...ctx,
session: { ...ctx.session, user: ctx.session.user },
},
});
});
Want results from more Discord servers?
Add your server