2025-06-30 06:36:03 -04:00
|
|
|
import NextAuth from 'next-auth';
|
|
|
|
|
import GoogleProvider from 'next-auth/providers/google';
|
|
|
|
|
import CredentialsProvider from 'next-auth/providers/credentials';
|
2025-06-30 20:26:00 -04:00
|
|
|
import { DrizzleAdapter } from '@auth/drizzle-adapter';
|
|
|
|
|
import { db } from '@/db';
|
|
|
|
|
import { users } from '@/db/schema';
|
|
|
|
|
import bcrypt from 'bcryptjs';
|
2025-06-30 06:36:03 -04:00
|
|
|
|
|
|
|
|
const handler = NextAuth({
|
2025-06-30 20:26:00 -04:00
|
|
|
adapter: DrizzleAdapter(db),
|
|
|
|
|
session: { strategy: 'jwt' },
|
2025-06-30 06:36:03 -04:00
|
|
|
providers: [
|
|
|
|
|
GoogleProvider({
|
|
|
|
|
clientId: process.env.GOOGLE_CLIENT_ID ?? '',
|
|
|
|
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? '',
|
|
|
|
|
}),
|
|
|
|
|
CredentialsProvider({
|
|
|
|
|
name: 'Credentials',
|
|
|
|
|
credentials: {
|
|
|
|
|
email: { label: "Email", type: "email" },
|
|
|
|
|
password: { label: "Password", type: "password" }
|
|
|
|
|
},
|
|
|
|
|
async authorize(credentials) {
|
2025-06-30 20:26:00 -04:00
|
|
|
console.log('Credentials received:', credentials);
|
2025-06-30 06:36:03 -04:00
|
|
|
if (!credentials?.email || !credentials?.password) return null;
|
2025-06-30 20:26:00 -04:00
|
|
|
// Query the real users table using Drizzle
|
|
|
|
|
const foundUser = await db.query.users.findFirst({
|
|
|
|
|
where: (u, { eq }) => eq(u.email, credentials.email),
|
|
|
|
|
});
|
|
|
|
|
console.log('User found:', foundUser);
|
|
|
|
|
if (foundUser && foundUser.hashedPassword && await bcrypt.compare(credentials.password, foundUser.hashedPassword)) {
|
|
|
|
|
console.log('Returning user:', foundUser);
|
|
|
|
|
return foundUser;
|
2025-06-30 06:36:03 -04:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
pages: {
|
|
|
|
|
signIn: '/account/login',
|
|
|
|
|
// signUp: '/account/register', // Uncomment when register page is ready
|
|
|
|
|
// error: '/account/error', // Uncomment when error page is ready
|
|
|
|
|
},
|
|
|
|
|
callbacks: {
|
2025-06-30 20:26:00 -04:00
|
|
|
async session({ session, user, token }) {
|
|
|
|
|
console.log('Session callback - user:', user);
|
|
|
|
|
console.log('Session callback - token:', token);
|
|
|
|
|
if (session.user) {
|
|
|
|
|
(session.user as any).isAdmin = (user as any)?.isAdmin ?? token?.isAdmin ?? false;
|
|
|
|
|
console.log('Session callback - final isAdmin:', (session.user as any).isAdmin);
|
|
|
|
|
}
|
2025-06-30 06:36:03 -04:00
|
|
|
return session;
|
|
|
|
|
},
|
|
|
|
|
async jwt({ token, user }) {
|
2025-06-30 20:26:00 -04:00
|
|
|
console.log('JWT callback - user:', user);
|
|
|
|
|
console.log('JWT callback - token before:', token);
|
|
|
|
|
if (user && typeof user === 'object' && 'isAdmin' in user) {
|
|
|
|
|
(token as any).isAdmin = (user as any).isAdmin;
|
|
|
|
|
console.log('JWT callback - setting isAdmin to:', (user as any).isAdmin);
|
2025-06-30 06:36:03 -04:00
|
|
|
}
|
2025-06-30 20:26:00 -04:00
|
|
|
console.log('JWT callback - token after:', token);
|
2025-06-30 06:36:03 -04:00
|
|
|
return token;
|
2025-06-30 20:26:00 -04:00
|
|
|
}
|
2025-06-30 06:36:03 -04:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export { handler as GET, handler as POST }
|