124_webcalibre2/src/auth.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2026-01-15 02:00:59 +00:00
// src/auth.ts
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" },
2026-01-21 01:34:02 +00:00
...authConfig, // This now spreads the default export from auth.config.ts
callbacks: {
async jwt({ token, account, user }) {
if (account) {
token.accessToken = account.access_token;
token.refreshToken = account.refresh_token;
token.expiresAt = account.expires_at;
}
if (user) {
token.sub = user.id;
2026-01-21 01:34:02 +00:00
// @ts-ignore
token.role = user.role;
}
return token;
},
async session({ session, token }) {
2026-01-15 02:00:59 +00:00
if (session?.user) {
session.user.id = token.sub as string;
2026-01-21 01:34:02 +00:00
// @ts-ignore
2026-01-15 02:00:59 +00:00
session.user.role = token.role as string;
session.accessToken = token.accessToken as string;
}
return session;
},
},
events: {
async linkAccount({ account, user }) {
console.log("🔗 Account linked successfully for user:", user.id);
if (!account.refresh_token) {
2026-01-21 01:34:02 +00:00
console.warn("⚠️ WARNING: No refresh_token received!");
}
}
}
});