124_webcalibre2/src/auth.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

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({
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!");
}
}
}
});