-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
48 lines (44 loc) · 1.19 KB
/
auth.js
File metadata and controls
48 lines (44 loc) · 1.19 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import bcrypt from "bcryptjs";
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { authConfig } from "./auth.config";
import { User } from "./model/user-model";
import dbConnect from "./service/mongo";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
...authConfig,
providers: [
Credentials({
async authorize(credentials) {
if (credentials === null) return null;
try {
await dbConnect();
const user = await User.findOne({ email: credentials.email });
console.log(user);
if (user) {
const isMatch = await bcrypt.compare(
credentials.password,
user.password
);
if (isMatch) {
return user;
} else {
console.error("password mismatch");
throw new Error("Check your password");
}
} else {
console.error("User not found");
throw new Error("User not found");
}
} catch (error) {
console.error(error);
throw new Error(error);
}
},
}),
],
});