55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import NextAuth from "next-auth";
|
|
import CredentialsProvider from "next-auth/providers/credentials";
|
|
import { compare } from "bcrypt";
|
|
import { DrizzleAdapter } from "@auth/drizzle-adapter";
|
|
import { db, connection } from "@/db/db";
|
|
import { users } from "@/db/schema";
|
|
import { eq } from "drizzle-orm";
|
|
import isValidCredentials from "zod";
|
|
|
|
const handler = NextAuth({
|
|
adapter: DrizzleAdapter(db),
|
|
session: {
|
|
strategy: "jwt",
|
|
},
|
|
providers: [
|
|
CredentialsProvider({
|
|
// The name to display on the sign in form (e.g. "Sign in with...")
|
|
name: "Credentials",
|
|
// `credentials` is used to generate a form on the sign in page.
|
|
// You can specify which fields should be submitted, by adding keys to the `credentials` object.
|
|
// e.g. domain, username, password, 2FA token, etc.
|
|
// You can pass any HTML attribute to the <input> tag through the object.
|
|
credentials: {
|
|
email: {},
|
|
password: {},
|
|
},
|
|
async authorize(credentials, req) {
|
|
// Check if input email is in the database and return user
|
|
const response = await db
|
|
.select()
|
|
.from(users)
|
|
.where(eq(users.email, `${credentials?.email}`));
|
|
|
|
if (!response) {
|
|
return null;
|
|
}
|
|
// compare hashed password with password entered
|
|
const passwordCheck = await compare(
|
|
`${credentials?.password}` || "",
|
|
response[0].password
|
|
);
|
|
|
|
console.log({ passwordCheck });
|
|
|
|
if (passwordCheck) {
|
|
return response[0];
|
|
}
|
|
return null;
|
|
},
|
|
}),
|
|
],
|
|
});
|
|
|
|
export { handler as GET, handler as POST };
|