You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import NextAuth from 'next-auth';
|
|
import { authConfig } from './auth.config';
|
|
import CredentialsProvider from 'next-auth/providers/credentials';
|
|
import bcrypt from 'bcryptjs';
|
|
import { prisma } from './lib/prisma';
|
|
|
|
export const { handlers, auth, signIn, signOut } = NextAuth({
|
|
...authConfig,
|
|
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 as string }
|
|
});
|
|
|
|
if (!user || !user.password) return null;
|
|
|
|
const passwordsMatch = await bcrypt.compare(
|
|
credentials.password as string,
|
|
user.password
|
|
);
|
|
|
|
if (passwordsMatch) return user;
|
|
|
|
return null;
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
async jwt({ token, user }) {
|
|
if (user) {
|
|
token.role = (user as any).role;
|
|
token.id = user.id;
|
|
}
|
|
return token;
|
|
},
|
|
async session({ session, token }) {
|
|
if (token) {
|
|
(session.user as any).role = token.role;
|
|
(session.user as any).id = token.id;
|
|
}
|
|
return session;
|
|
},
|
|
},
|
|
});
|