NeahNew/lib/auth.ts
2025-05-05 19:13:57 +02:00

77 lines
1.9 KiB
TypeScript

import { NextAuthOptions } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import { prisma } from '@/lib/prisma';
// We don't need to extend User or Session here as they're defined in types/next-auth.d.ts
// Removing the conflicting declarations
export const authOptions: NextAuthOptions = {
providers: [
CredentialsProvider({
name: 'Credentials',
credentials: {
email: { label: 'Email', type: 'email' },
password: { label: 'Password', type: 'password' }
},
async authorize(credentials) {
if (!credentials?.email || !credentials?.password) {
return null;
}
const user = await prisma.user.findUnique({
where: { email: credentials.email },
select: {
id: true,
email: true,
password: true
}
});
if (!user) {
return null;
}
// In production, you should use proper password hashing
if (user.password !== credentials.password) {
return null;
}
// Return a user object compatible with the User interface in types/next-auth.d.ts
return {
id: user.id,
email: user.email,
name: user.email.split('@')[0],
first_name: '',
last_name: '',
username: user.email.split('@')[0],
role: ['user'],
image: null
};
}
})
],
session: {
strategy: 'jwt',
maxAge: 30 * 24 * 60 * 60, // 30 days
},
jwt: {
maxAge: 30 * 24 * 60 * 60, // 30 days
},
pages: {
signIn: '/login',
},
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id;
}
return token;
},
async session({ session, token }) {
if (session.user) {
session.user.id = token.id as string;
}
return session;
}
}
};