81 lines
1.7 KiB
TypeScript
81 lines
1.7 KiB
TypeScript
import { NextAuthOptions } from 'next-auth';
|
|
import CredentialsProvider from 'next-auth/providers/credentials';
|
|
import { prisma } from '@/lib/prisma';
|
|
|
|
// Extend the built-in User type
|
|
declare module "next-auth" {
|
|
interface User {
|
|
id: string;
|
|
email: string;
|
|
name?: string;
|
|
}
|
|
|
|
interface Session {
|
|
user: User;
|
|
}
|
|
}
|
|
|
|
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 {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.email.split('@')[0]
|
|
};
|
|
}
|
|
})
|
|
],
|
|
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;
|
|
}
|
|
}
|
|
};
|