-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathauth.config.ts
31 lines (30 loc) · 1.01 KB
/
auth.config.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
import bcryptjs from "bcryptjs";
import type { NextAuthConfig } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import Github from "next-auth/providers/github";
import Google from "next-auth/providers/google";
import { InvalidCredentials, UserNotFound } from "./lib/auth";
import { CredentialsSchema } from "./schemas/auth";
import { findUserbyEmail } from "./services";
export default {
providers: [
Credentials({
async authorize(credentials) {
const validdCredentials = CredentialsSchema.safeParse(credentials);
if (validdCredentials.success) {
const { email, password } = validdCredentials.data;
const user = await findUserbyEmail(email);
if (!user || !user.password) {
throw new UserNotFound();
}
const validPassword = await bcryptjs.compare(password, user.password);
if (validPassword) return user;
}
//TODO: Would it be better to throw new InvalidCredential?
return null;
},
}),
Google,
Github,
],
} satisfies NextAuthConfig;