-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
74 lines (71 loc) · 2.42 KB
/
auth.js
File metadata and controls
74 lines (71 loc) · 2.42 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
import FacebookProvider from 'next-auth/providers/facebook';
import GithubProvider from 'next-auth/providers/github';
import TwitterProvider from 'next-auth/providers/twitter';
import { MongoDBAdapter } from '@auth/mongodb-adapter';
import mongoClientPromise from './lib/mongoClientPromise';
import CredentialProvider from 'next-auth/providers/credentials';
import { userModel } from './models/user-model';
import { dbConnect } from './lib/mongo';
export const {
handlers: { GET, POST }, // for providing custom routes
auth,
signIn,
signOut
} = NextAuth({
adapter: MongoDBAdapter(mongoClientPromise, {
databaseName: process.env.NODE_ENV
}),
session: {
strategy: 'jwt'
},
providers: [
CredentialProvider({
credentials: {
email: {},
password: {}
},
async authorize(credentials) {
if (credentials === null) {
return null;
}
await dbConnect();
try {
const user = await userModel.findOne({
email: credentials?.email
});
if (user) {
const isMatch =
user?.password === credentials?.password;
if (isMatch) {
return user;
} else {
throw new Error('Invalid credentials');
}
} else {
throw new Error('Invalid credentials');
}
} 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
}),
GithubProvider({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET
}),
TwitterProvider({
clientId: process.env.TWITTER_CLIENT_ID,
clientSecret: process.env.TWITTER_CLIENT_SECRET
})
]
});