-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathauth.config.ts
More file actions
31 lines (30 loc) · 1.02 KB
/
Copy pathauth.config.ts
File metadata and controls
31 lines (30 loc) · 1.02 KB
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 validatedCredentials = CredentialsSchema.safeParse(credentials);
if (validatedCredentials.success) {
const { email, password } = validatedCredentials.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;