Skip to content

Instantly share code, notes, and snippets.

@jsstoni
Created April 3, 2024 22:08
Show Gist options
  • Save jsstoni/93eb842ecdb1e54fcfb0e6eddf86b9da to your computer and use it in GitHub Desktop.
Save jsstoni/93eb842ecdb1e54fcfb0e6eddf86b9da to your computer and use it in GitHub Desktop.
[...nextauth]
import NextAuth from "next-auth";
import CredentialProvider from "next-auth/providers/credentials";
import prisma from "@/libs/prisma";
import bcrypt from "bcrypt";
interface Credentials {
email: string;
password: string;
}
export const authOptions = {
providers: [
CredentialProvider({
name: "Credentials",
credentials: {
email: { label: "email", type: "text" },
password: {
label: "password",
type: "password",
},
},
async authorize(credentials) {
const { email, password } = credentials as Credentials;
const user = await prisma.users.findUnique({
where: { email: email },
});
if (!user) throw new Error("No user found");
const verifyPassword = await bcrypt.compare(password, user.password);
if (!verifyPassword) throw new Error("Wrong password");
return {
id: user.id.toString(),
};
},
}),
],
pages: {
signIn: "/login",
},
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment