-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathroute.js
57 lines (51 loc) · 1.43 KB
/
route.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
52
53
54
55
56
57
import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
import CredentialsProvider from "next-auth/providers/credentials";
import User from "@/models/User";
import connect from "@/utils/db";
import bcrypt from "bcryptjs";
const handler = NextAuth({
providers: [
CredentialsProvider({
id: "credentials",
name: "Credentials",
async authorize(credentials) {
//Check if the user exists.
await connect();
try {
const user = await User.findOne({
email: credentials.email,
});
if (user) {
const isPasswordCorrect = await bcrypt.compare(
credentials.password,
user.password
);
if (isPasswordCorrect) {
return user;
} else {
throw new Error("Wrong Credentials!");
}
} else {
throw new Error("User not found!");
}
} catch (err) {
throw new Error(err);
}
},
}),
GithubProvider({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
],
pages: {
error: "/dashboard/login",
},
});
export { handler as GET, handler as POST };