-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
63 lines (59 loc) · 1.96 KB
/
auth.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
58
59
60
61
62
63
import { MongoDBAdapter } from "@auth/mongodb-adapter";
import bcrypt from "bcryptjs";
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import FacebookProvider from "next-auth/providers/facebook";
import GoogleProvider from "next-auth/providers/google";
import mongoClientPromise from "./database/mongoClientPromise";
import { userModel } from "./models/user-model";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
adapter: MongoDBAdapter(
mongoClientPromise,
{ databaseName: process.env.ENVIRONMENT }
),
session: {
strategy: 'jwt',
},
providers: [
CredentialsProvider({
credentials: {
email: {},
password: {},
},
async authorize(credentials) {
if (credentials == null) return null;
try {
const user = await userModel.findOne({ email: credentials.email });
if (user) {
const isMatch = await bcrypt.compare(
credentials.password,
user.password
);
if (isMatch) {
return user;
} else {
throw new Error('Email or password mismatch');
}
} else {
throw new Error('User not found');
}
} catch (error) {
throw new Error(error);
}
}
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
}),
]
})