76 lines
1.6 KiB
TypeScript
76 lines
1.6 KiB
TypeScript
import { NextAuthOptions } from 'next-auth';
|
|
import CredentialsProvider from 'next-auth/providers/credentials';
|
|
import { compare } from 'bcryptjs';
|
|
import { prisma } from './prisma';
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
session: {
|
|
strategy: 'jwt',
|
|
},
|
|
pages: {
|
|
signIn: '/login',
|
|
},
|
|
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,
|
|
},
|
|
});
|
|
|
|
if (!user || !user.active) {
|
|
return null;
|
|
}
|
|
|
|
const isPasswordValid = await compare(
|
|
credentials.password,
|
|
user.password
|
|
);
|
|
|
|
if (!isPasswordValid) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
role: user.role,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
return {
|
|
...token,
|
|
id: user.id,
|
|
role: user.role,
|
|
};
|
|
}
|
|
return token;
|
|
},
|
|
async session({ session, token }) {
|
|
return {
|
|
...session,
|
|
user: {
|
|
...session.user,
|
|
id: token.id,
|
|
role: token.role,
|
|
},
|
|
};
|
|
},
|
|
},
|
|
};
|