Files
next-learn/dashboard/15-final/app/api/auth/[...nextauth]/route.ts
2023-09-20 13:56:30 -05:00

54 lines
1.3 KiB
TypeScript

import NextAuth, { NextAuthOptions } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import bcrypt from 'bcrypt';
import { fetchUser } from '../../../lib/data';
import { User } from '@/app/lib/definitions';
const authOptions: NextAuthOptions = {
providers: [
CredentialsProvider({
name: 'credentials',
credentials: {},
// TODO: Not sure how to type authorize function correctly
// @ts-ignore
async authorize(credentials: {
email: string;
password: string;
}): Promise<User | null | undefined> {
const { email, password } = credentials;
try {
const user = await fetchUser(email);
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: '/login',
},
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST, authOptions };