/admin and with working with db. data is pulling from db

This commit is contained in:
2025-06-30 20:26:00 -04:00
parent 5c046874a8
commit b478d9797d
33 changed files with 1214 additions and 58 deletions

View File

@@ -1,16 +1,14 @@
import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
import CredentialsProvider from 'next-auth/providers/credentials';
// In-memory user store (for demo only)
type User = { email: string; password: string };
declare global {
// eslint-disable-next-line no-var
var _users: User[] | undefined;
}
const users: User[] = global._users || (global._users = []);
import { DrizzleAdapter } from '@auth/drizzle-adapter';
import { db } from '@/db';
import { users } from '@/db/schema';
import bcrypt from 'bcryptjs';
const handler = NextAuth({
adapter: DrizzleAdapter(db),
session: { strategy: 'jwt' },
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID ?? '',
@@ -23,25 +21,16 @@ const handler = NextAuth({
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
console.log('Credentials received:', credentials);
if (!credentials?.email || !credentials?.password) return null;
// Check in-memory user store
const user = users.find(
(u) => u.email === credentials.email && u.password === credentials.password
);
if (user) {
return {
id: user.email,
email: user.email,
name: user.email.split('@')[0],
};
}
// For demo, still allow the test user
if (credentials.email === "test@example.com" && credentials.password === "password") {
return {
id: "1",
email: credentials.email,
name: "Test User",
};
// 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;
}
return null;
}
@@ -53,17 +42,25 @@ const handler = NextAuth({
// error: '/account/error', // Uncomment when error page is ready
},
callbacks: {
async session({ session, token }) {
// Add any additional user data to the session here
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);
}
return session;
},
async jwt({ token, user }) {
// Add any additional user data to the JWT here
if (user) {
token.id = user.id;
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);
}
console.log('JWT callback - token after:', token);
return token;
},
}
},
})