-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy path[...nextauth].js
110 lines (106 loc) · 3.41 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
101
102
103
104
105
106
107
108
109
110
import NextAuth from "next-auth";
import WikimediaProvider from "next-auth/providers/wikimedia";
import winston from "winston";
const logger = winston.loggers.get("defaultLogger");
async function refetchAccessToken(refreshToken) {
try {
const response = await fetch(
process.env.NEXT_PUBLIC_WIKIMEDIA_URL + "/w/rest.php/oauth2/access_token",
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
grant_type: "refresh_token",
refresh_token: refreshToken,
client_id: process.env.WIKIMEDIA_CLIENT_ID,
client_secret: process.env.WIKIMEDIA_CLIENT_SECRET,
}),
}
);
return await response.json();
} catch (error) {
logger.log({
level: "error",
message: `refetchAccessToken: ${error}`,
});
throw error;
}
}
export const authOptions = {
providers: [
WikimediaProvider({
clientId: process.env.WIKIMEDIA_CLIENT_ID,
clientSecret: process.env.WIKIMEDIA_CLIENT_SECRET,
token: `${process.env.NEXT_PUBLIC_WIKIMEDIA_URL}/w/rest.php/oauth2/access_token`,
userinfo: `${process.env.NEXT_PUBLIC_WIKIMEDIA_URL}/w/rest.php/oauth2/resource/profile`,
authorization: {
url: `${process.env.NEXT_PUBLIC_WIKIMEDIA_URL}/w/rest.php/oauth2/authorize`,
},
}),
],
session: {
jwt: true,
},
debug: process.env.NODE_ENV !== "production",
logger: {
debug(code, metadata) {
// store logs for every user logging in using OAuth
if (code === "OAUTH_CALLBACK_RESPONSE" && metadata.account.access_token) {
logger.log({
level: "info",
message: `User ${metadata.profile.name} logged in using ${
metadata.account.provider.charAt(0).toUpperCase() +
metadata.account.provider.slice(1)
} OAuth`,
});
}
},
error(code, metadata) {
// store logs of aborted logins by users using OAuth
if (code === "OAUTH_CALLBACK_HANDLER_ERROR") {
logger.log({
level: "error",
message: `[${code}] ${metadata.error_description}`,
});
}
},
},
callbacks: {
async jwt({ token, account }) {
const threeHoursThirtyMinutesInMilliseconds = 12600000;
if (account) {
token.accessToken = account.access_token;
token.refreshToken = account.refresh_token;
token.expiresIn = Date.now() + threeHoursThirtyMinutesInMilliseconds;
}
// Refresh the token if it's expired
if (token.expiresIn && Date.now() > token.expiresIn) {
try {
const new_session = await refetchAccessToken(token.refreshToken);
if (new_session.access_token) {
token.accessToken = new_session.access_token;
token.refreshToken = new_session.refresh_token;
token.expiresIn =
Date.now() + threeHoursThirtyMinutesInMilliseconds;
}
return token;
} catch (error) {
logger.log({
level: "error",
message: `jwt callback: ${error}`,
});
}
}
return token;
},
async session({ session, token, user }) {
// Add the access token to the session object
session.accessToken = token.accessToken;
session.expiresIn = token.expiresIn;
return session;
},
},
};
export default (req, res) => NextAuth(req, res, authOptions);