-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.config.js
51 lines (41 loc) · 1.67 KB
/
auth.config.js
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
45
46
47
48
49
50
51
import Credentials from "next-auth/providers/credentials";
import Google from "next-auth/providers/google";
import { loginSchema } from "./components/Forms/schema/loginSchema";
import { getUserEmail } from "./data/user";
import bcrypt from "bcryptjs";
import { db } from "./lib/db";
import { revalidatePath } from "next/cache";
const configuration = {
providers: [
Google({
clientId: process.env.AUTH_GOOGLE_ID,
clientSecret: process.env.AUTH_GOOGLE_SECRET,
}),
Credentials({
async authorize(credentials){
const validatedFields = loginSchema.safeParse(credentials);
if(validatedFields.success){
const { email, password } = validatedFields.data;
// Checks if the user exists in the database
const user = await getUserEmail(email);
if(!user || !user.password) return null;
const passwordsMatch = await bcrypt.compare(password, user.password);
if(passwordsMatch){
await db.ActivityLogs.create({
data: {
userId: user.id,
action: "Login",
name: user.firstName + " " + user.lastName,
information: "User logged in",
}
})
revalidatePath("/dashboard/system-log");
return user;
}
}
return null;
}
}),
],
}
export default configuration;