-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[...nextauth].js
100 lines (88 loc) · 2.35 KB
/
[...nextauth].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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* eslint-disable no-param-reassign */
import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
import Adapters from 'next-auth/adapters';
import sendVerificationRequestOverride from '../../../utils/verificationUtil';
import Models from '../../../models';
const options = {
providers: [
Providers.Email({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
secure: true,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
sendVerificationRequest: sendVerificationRequestOverride,
}),
],
adapter: Adapters.TypeORM.Adapter(
{
type: 'mongodb',
url: process.env.MONGODB_URI,
w: 'majority',
useNewUrlParser: true,
useUnifiedTopology: true,
retryWrites: true,
},
{
models: {
User: Models.User,
},
},
),
debug: true,
session: {
jwt: true,
maxAge: 30 * 24 * 60 * 60, // 30 days
updateAge: 24 * 60 * 60, // 24 hours
},
secret: process.env.AUTH_SECRET,
jwt: {
secret: process.env.AUTH_SECRET,
raw: true,
},
pages: {
newUser: '/user/newuser',
signIn: '/auth/signin',
verifyRequest: '/auth/verify-request',
},
callbacks: {
jwt: async (token, user) => {
const isSignIn = !!(user);
if (isSignIn) {
token.auth_time = Number(new Date());
token.id = user.id;
}
return Promise.resolve(token);
},
session: async (session, sessionToken) => {
const { id } = sessionToken;
const url = `${process.env.SITE}/api/user/${id}`;
// eslint-disable-next-line no-undef
const res = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
});
if (res.status === 200) {
const user = await res.json();
session.user.id = id;
session.user.name = user.name;
session.user.firstName = user.firstName;
session.user.groups = user.groups;
session.user.role = user.role ? user.role : 'user';
} else {
return Promise.reject();
}
return Promise.resolve(session);
},
},
};
export default (req, res) => NextAuth(req, res, options);