mirror of
https://github.com/vercel/next-learn.git
synced 2026-07-02 08:58:39 +00:00
48 lines
1.1 KiB
TypeScript
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 };
|