sanketcharanpahadi
sanketcharanpahadi
TTCTheo's Typesafe Cult
Created by bastián on 6/22/2024 in #questions
T3 Middleware
//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)(.*)"],
};
5 replies
TTCTheo's Typesafe Cult
Created by bastián on 6/22/2024 in #questions
T3 Middleware
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.
5 replies