Files
next-learn/dashboard/15-final/app/api/auth/[...nextauth]/route.ts
2023-09-20 10:49:36 -05:00

48 lines
1.1 KiB
TypeScript

import NextAuth from 'next-auth/next';
import CredentialsProvider from 'next-auth/providers/credentials';
import bcrypt from 'bcrypt';
import { fetchUser } from '../../../lib/data';
export const authOptions = {
providers: [
CredentialsProvider({
name: 'credentials',
credentials: {},
async authorize(credentials) {
const { email, password } = credentials;
try {
const user = await fetchUser(email);
console.log('user: ', user);
if (!user) {
return null;
}
const passwordsMatch = await bcrypt.compare(password, user.password);
if (!passwordsMatch) {
console.log('Passwords do not match');
return null;
}
return user;
} catch (error) {
console.log('Error: ', error);
}
},
}),
],
session: {
strategy: 'jwt',
},
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn: '/',
},
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };