2026-01-08 05:41:31 +00:00
|
|
|
import NextAuth from "next-auth";
|
|
|
|
|
import { PrismaAdapter } from "@auth/prisma-adapter";
|
|
|
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
|
import authConfig from "./auth.config";
|
|
|
|
|
|
2026-01-06 02:40:21 +00:00
|
|
|
export const { handlers, signIn, signOut, auth } = NextAuth({
|
2026-01-08 05:41:31 +00:00
|
|
|
adapter: PrismaAdapter(prisma),
|
|
|
|
|
session: { strategy: "jwt" },
|
|
|
|
|
...authConfig,
|
|
|
|
|
callbacks: {
|
|
|
|
|
async jwt({ token, account, user }) {
|
|
|
|
|
// On the first sign in, 'account' contains the refresh_token
|
|
|
|
|
if (account) {
|
|
|
|
|
token.accessToken = account.access_token;
|
|
|
|
|
token.refreshToken = account.refresh_token;
|
|
|
|
|
token.expiresAt = account.expires_at;
|
|
|
|
|
}
|
|
|
|
|
if (user) {
|
|
|
|
|
token.sub = user.id;
|
|
|
|
|
}
|
|
|
|
|
return token;
|
|
|
|
|
},
|
|
|
|
|
async session({ session, token }) {
|
|
|
|
|
if (session?.user && token.sub) {
|
|
|
|
|
session.user.id = token.sub;
|
|
|
|
|
}
|
|
|
|
|
return session;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
// Adding events can help debug if the account is actually linking
|
|
|
|
|
events: {
|
|
|
|
|
async linkAccount({ account, user }) {
|
|
|
|
|
console.log("🔗 Account linked successfully for user:", user.id);
|
|
|
|
|
if (!account.refresh_token) {
|
|
|
|
|
console.warn("⚠️ WARNING: No refresh_token received in linkAccount event!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|