create-auth-js-boiler
Version:
Create a new auth-js-boiler project
48 lines (44 loc) • 1.5 kB
text/typescript
import Google from "next-auth/providers/google";
import Credentials from "next-auth/providers/credentials";
import type { NextAuthConfig } from "next-auth";
import { prisma } from "./lib/prisma";
import bcrypt from "bcryptjs";
import { loginSchema } from "./schemas/auth.schema";
import { User } from "next-auth";
export default {
providers: [
Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
Credentials({
// some user can bypass the login form so we need to validate the credentials
async authorize(credentials) {
const validatedFields = loginSchema.safeParse(credentials);
if (validatedFields.success) {
const { email, password } = validatedFields.data;
const user = await prisma.user.findUnique({
where: {
email,
},
});
if (!user || !user.password) return null;
const passwordMatch = await bcrypt.compare(password, user.password);
const existingAccount = await prisma.account.findFirst({
where: { id: user.id },
});
if (passwordMatch) {
return {
id: user.id,
name: user.name,
email: user.email,
image: user.image,
role: user.role,
isTwoFactorEnabled: !!existingAccount,
} as User;
}
}
},
}),
],
} satisfies NextAuthConfig;