Files
opengifame/pages/api/auth/[...nextauth].ts
2022-10-05 22:05:13 +01:00

49 lines
1.4 KiB
TypeScript

import NextAuth, {NextAuthOptions} from "next-auth";
import {PrismaAdapter} from "@next-auth/prisma-adapter";
import CredentialsProvider from "next-auth/providers/credentials";
import prisma from "@lib/prismadb";
import {confirmPassword, hashPassword} from "@lib/crypt";
import {has, omit} from "lodash";
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
session: {
strategy: "jwt",
},
secret: process.env.SECRET_KEY,
providers: [
CredentialsProvider({
type: "credentials",
credentials: {},
authorize: async (credentials, request) => {
if (!credentials) {
return null;
}
const user = await prisma.user.findUnique({
where: {email: credentials.email},
select: {
id: true,
email: true,
password: true,
},
});
const hashed = await confirmPassword(credentials.password, user.password);
if (hashed) {
return omit(user, "password")
}
return null;
},
}),
],
pages: {
signIn: "/auth/signin",
signOut: "/auth/signout",
error: "/auth/error", // Error code passed in query string as ?error=
verifyRequest: "/auth/verify-request", // (used for check email message)
newUser: "/auth/new-user", // New users will be directed here on first sign in (leave the property out if not of interest)
},
};
export default NextAuth(authOptions);