Created
April 3, 2024 22:08
-
-
Save jsstoni/93eb842ecdb1e54fcfb0e6eddf86b9da to your computer and use it in GitHub Desktop.
[...nextauth]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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