74 lines
1.7 KiB
TypeScript
74 lines
1.7 KiB
TypeScript
import { NextAuthOptions } from 'next-auth';
|
|
import CredentialsProvider from 'next-auth/providers/credentials';
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
declare module 'next-auth' {
|
|
interface User {
|
|
id: string;
|
|
email: string;
|
|
password: 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;
|
|
}
|
|
|
|
// Find user in database
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: credentials.email },
|
|
});
|
|
|
|
if (!user) {
|
|
return null;
|
|
}
|
|
|
|
// Here you would typically verify the password
|
|
// For now, we'll just return the user
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
password: user.password,
|
|
};
|
|
}
|
|
})
|
|
],
|
|
session: {
|
|
strategy: 'jwt',
|
|
},
|
|
pages: {
|
|
signIn: '/login',
|
|
},
|
|
callbacks: {
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
token.id = user.id;
|
|
token.email = user.email;
|
|
token.password = user.password;
|
|
}
|
|
return token;
|
|
},
|
|
async session({ session, token }) {
|
|
if (token) {
|
|
session.user.id = token.id as string;
|
|
session.user.email = token.email as string;
|
|
session.user.password = token.password as string;
|
|
}
|
|
return session;
|
|
}
|
|
}
|
|
};
|