T3 Middleware

Hello there, I was wondering if it is possible to use middleware.ts using the t3 stack (NextJS, tRPC, Drizzle and AuthJS v4). There are two session strategies, database sessions (t3 default) and JWT sessions. I read a lot and saw some videos but still not sure if it is possible. Maybe with the AuthJS v5 something change or idk. Does someone know about this? I already read the t3 documentation about this this
2 Replies
bastián
bastián2mo ago
Something important: I don't want to use JWT, the question is ¿Is it possible to use middleware.ts with the database strategy? thanks.
sanketcharanpahadi
Hey @bastián . I tried using nextAuth v5 (beta) and the default database session strategy was not working for me. I have to do it with the jwt sessions. When I am using using the default database session , it was throwing error. Last thing, I am not using tRPC.
//src/auth.config.ts
`import type { NextAuthConfig } from "next-auth";
import Google from "next-auth/providers/google";
import { env } from "./env";

const nextAuthConfig: NextAuthConfig = {
providers: [
Google({
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
}),
],
};
export default nextAuthConfig;
//src/auth.config.ts
`import type { NextAuthConfig } from "next-auth";
import Google from "next-auth/providers/google";
import { env } from "./env";

const nextAuthConfig: NextAuthConfig = {
providers: [
Google({
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
}),
],
};
export default nextAuthConfig;
//src/auth.ts
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import NextAuth from "next-auth";
import { db } from "./server/db";
import { accounts, sessions, users } from "./server/db/schema";
import nextAuthConfig from "./auth.config";

export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: DrizzleAdapter(db, {
usersTable: users,
accountsTable: accounts,
sessionsTable: sessions,
}),
...nextAuthConfig,
session: { strategy: "jwt" },
});
//src/auth.ts
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import NextAuth from "next-auth";
import { db } from "./server/db";
import { accounts, sessions, users } from "./server/db/schema";
import nextAuthConfig from "./auth.config";

export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: DrizzleAdapter(db, {
usersTable: users,
accountsTable: accounts,
sessionsTable: sessions,
}),
...nextAuthConfig,
session: { strategy: "jwt" },
});
//src/middleware.ts
import NextAuth from "next-auth";
import nextAuthConfig from "./auth.config";
import { NextResponse } from "next/server";

const { auth } = NextAuth(nextAuthConfig);

export default auth((req) => {
const { nextUrl } = req;
const isLoggedIn = !!req.auth;

console.log(isLoggedIn, nextUrl);
return NextResponse.next();
});

export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};
//src/middleware.ts
import NextAuth from "next-auth";
import nextAuthConfig from "./auth.config";
import { NextResponse } from "next/server";

const { auth } = NextAuth(nextAuthConfig);

export default auth((req) => {
const { nextUrl } = req;
const isLoggedIn = !!req.auth;

console.log(isLoggedIn, nextUrl);
return NextResponse.next();
});

export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};