62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import { PrismaAdapter } from "@auth/prisma-adapter";
|
|
import bcrypt from "bcryptjs";
|
|
import NextAuth from "next-auth";
|
|
import Credentials from "next-auth/providers/credentials";
|
|
import Google from "next-auth/providers/google";
|
|
import { prisma } from "@/lib/prisma.server";
|
|
import { authConfig } from "./auth.config";
|
|
|
|
export const { handlers, signIn, signOut, auth } = NextAuth({
|
|
adapter: PrismaAdapter(prisma),
|
|
...authConfig,
|
|
providers: [
|
|
Google({
|
|
clientId: process.env.GOOGLE_CLIENT_ID ?? "",
|
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? "",
|
|
authorization: {
|
|
params: {
|
|
scope:
|
|
"openid https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/calendar.events",
|
|
access_type: "offline",
|
|
prompt: "consent",
|
|
},
|
|
},
|
|
}),
|
|
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 isPasswordValid = await bcrypt.compare(
|
|
credentials.password as string,
|
|
user.password,
|
|
);
|
|
|
|
if (!isPasswordValid) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
image: user.image,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
});
|