-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
44 lines (37 loc) · 1.17 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { authConfig } from "./auth.config";
import { z } from "zod";
import { eq } from "drizzle-orm";
import { db } from "./lib/db";
import { users } from "./lib/db/schema/users";
async function getUser(email: string) {
// Query the users_table by email
const user = await db
.select()
.from(users)
.where(eq(users.email, email))
.limit(1); // Limit to one user, as email is unique
return user[0] || null; // Return the first result or null if no user found
}
export const { handlers, auth, signIn, signOut } = NextAuth({
...authConfig,
providers: [
Credentials({
async authorize(credentials) {
const parsedCredentials = z
.object({ email: z.string().email(), password: z.string().min(6) })
.safeParse(credentials);
if (parsedCredentials.success) {
const { email, password } = parsedCredentials.data;
const user = await getUser(email);
if (!user) return null;
if (password === user.password) return { ...user, id: user.id.toString() };
console.log("Invalid credentials ✋");
return null;
}
return null;
},
}),
],
});