1
- import adapters from " ../adapters"
2
- import jwt from " ../lib/jwt"
3
- import parseUrl from " ../lib/parse-url"
4
- import logger , { setLogger } from " ../lib/logger"
5
- import * as cookie from " ./lib/cookie"
6
- import * as defaultEvents from " ./lib/default-events"
7
- import * as defaultCallbacks from " ./lib/default-callbacks"
8
- import parseProviders from " ./lib/providers"
9
- import * as routes from " ./routes"
10
- import renderPage from " ./pages"
11
- import createSecret from " ./lib/create-secret"
12
- import callbackUrlHandler from " ./lib/callback-url-handler"
13
- import extendRes from " ./lib/extend-res"
14
- import csrfTokenHandler from " ./lib/csrf-token-handler"
15
- import * as pkce from " ./lib/oauth/pkce-handler"
16
- import * as state from " ./lib/oauth/state-handler"
1
+ import adapters from ' ../adapters'
2
+ import jwt from ' ../lib/jwt'
3
+ import parseUrl from ' ../lib/parse-url'
4
+ import logger , { setLogger } from ' ../lib/logger'
5
+ import * as cookie from ' ./lib/cookie'
6
+ import * as defaultEvents from ' ./lib/default-events'
7
+ import * as defaultCallbacks from ' ./lib/default-callbacks'
8
+ import parseProviders from ' ./lib/providers'
9
+ import * as routes from ' ./routes'
10
+ import renderPage from ' ./pages'
11
+ import createSecret from ' ./lib/create-secret'
12
+ import callbackUrlHandler from ' ./lib/callback-url-handler'
13
+ import extendRes from ' ./lib/extend-res'
14
+ import csrfTokenHandler from ' ./lib/csrf-token-handler'
15
+ import * as pkce from ' ./lib/oauth/pkce-handler'
16
+ import * as state from ' ./lib/oauth/state-handler'
17
17
18
18
// To work properly in production with OAuth providers the NEXTAUTH_URL
19
19
// environment variable must be set.
20
20
if ( ! process . env . NEXTAUTH_URL ) {
21
- logger . warn ( " NEXTAUTH_URL" , " NEXTAUTH_URL environment variable not set" )
21
+ logger . warn ( ' NEXTAUTH_URL' , ' NEXTAUTH_URL environment variable not set' )
22
22
}
23
23
24
24
/**
25
25
* @param {import("next").NextApiRequest } req
26
26
* @param {import("next").NextApiResponse } res
27
27
* @param {import("types").NextAuthOptions } userOptions
28
28
*/
29
- async function NextAuthHandler ( req , res , userOptions ) {
29
+ async function NextAuthHandler ( req , res , userOptions ) {
30
30
if ( userOptions . logger ) {
31
31
setLogger ( userOptions . logger )
32
32
}
@@ -39,54 +39,44 @@ async function NextAuthHandler(req, res, userOptions) {
39
39
// to avoid early termination of calls to the serverless function
40
40
// (and then return that promise when we are done) - eslint
41
41
// complains but I'm not sure there is another way to do this.
42
- // eslint-disable-next-line no-async-promise-executor
43
- return new Promise ( async ( resolve ) => {
42
+ return new Promise ( async resolve => { // eslint-disable-line no-async-promise-executor
44
43
extendRes ( req , res , resolve )
45
44
46
45
if ( ! req . query . nextauth ) {
47
- const error =
48
- "Cannot find [...nextauth].js in pages/api/auth. Make sure the filename is written correctly."
46
+ const error = 'Cannot find [...nextauth].js in pages/api/auth. Make sure the filename is written correctly.'
49
47
50
- logger . error ( " MISSING_NEXTAUTH_API_ROUTE_ERROR" , error )
48
+ logger . error ( ' MISSING_NEXTAUTH_API_ROUTE_ERROR' , error )
51
49
return res . status ( 500 ) . end ( `Error: ${ error } ` )
52
50
}
53
51
54
52
const {
55
53
nextauth,
56
54
action = nextauth [ 0 ] ,
57
55
providerId = nextauth [ 1 ] ,
58
- error = nextauth [ 1 ] ,
56
+ error = nextauth [ 1 ]
59
57
} = req . query
60
58
61
59
// @todo refactor all existing references to baseUrl and basePath
62
- const { basePath, baseUrl } = parseUrl (
63
- process . env . NEXTAUTH_URL || process . env . VERCEL_URL
64
- )
60
+ const { basePath, baseUrl } = parseUrl ( process . env . NEXTAUTH_URL || process . env . VERCEL_URL )
65
61
66
62
const cookies = {
67
- ...cookie . defaultCookies (
68
- userOptions . useSecureCookies || baseUrl . startsWith ( "https://" )
69
- ) ,
63
+ ...cookie . defaultCookies ( userOptions . useSecureCookies || baseUrl . startsWith ( 'https://' ) ) ,
70
64
// Allow user cookie options to override any cookie settings above
71
- ...userOptions . cookies ,
65
+ ...userOptions . cookies
72
66
}
73
67
74
68
const secret = createSecret ( { userOptions, basePath, baseUrl } )
75
69
76
- const providers = parseProviders ( {
77
- providers : userOptions . providers ,
78
- baseUrl,
79
- basePath,
80
- } )
70
+ const providers = parseProviders ( { providers : userOptions . providers , baseUrl, basePath } )
81
71
const provider = providers . find ( ( { id } ) => id === providerId )
82
72
83
73
// Protection only works on OAuth 2.x providers
84
- if ( provider ?. type === " oauth" && provider . version ?. startsWith ( "2" ) ) {
74
+ if ( provider ?. type === ' oauth' && provider . version ?. startsWith ( '2' ) ) {
85
75
// When provider.state is undefined, we still want this to pass
86
76
if ( ! provider . protection && provider . state !== false ) {
87
77
// Default to state, as we did in 3.1 REVIEW: should we use "pkce" or "none" as default?
88
- provider . protection = [ " state" ]
89
- } else if ( typeof provider . protection === " string" ) {
78
+ provider . protection = [ ' state' ]
79
+ } else if ( typeof provider . protection === ' string' ) {
90
80
provider . protection = [ provider . protection ]
91
81
}
92
82
}
@@ -96,16 +86,14 @@ async function NextAuthHandler(req, res, userOptions) {
96
86
// Parse database / adapter
97
87
// If adapter is provided, use it (advanced usage, overrides database)
98
88
// If database URI or config object is provided, use it (simple usage)
99
- const adapter =
100
- userOptions . adapter ??
101
- ( userOptions . database && adapters . Default ( userOptions . database ) )
89
+ const adapter = userOptions . adapter ?? ( userOptions . database && adapters . Default ( userOptions . database ) )
102
90
103
91
// User provided options are overriden by other options,
104
92
// except for the options with special handling above
105
93
req . options = {
106
94
debug : false ,
107
95
pages : { } ,
108
- theme : " auto" ,
96
+ theme : ' auto' ,
109
97
// Custom options override defaults
110
98
...userOptions ,
111
99
// These computed settings can have values in userOptions but we override them
@@ -123,28 +111,28 @@ async function NextAuthHandler(req, res, userOptions) {
123
111
jwt : ! adapter , // If no adapter specified, force use of JSON Web Tokens (stateless)
124
112
maxAge,
125
113
updateAge : 24 * 60 * 60 , // Sessions updated only if session is greater than this value (0 = always, 24*60*60 = every 24 hours)
126
- ...userOptions . session ,
114
+ ...userOptions . session
127
115
} ,
128
116
// JWT options
129
117
jwt : {
130
118
secret, // Use application secret if no keys specified
131
119
maxAge, // same as session maxAge,
132
120
encode : jwt . encode ,
133
121
decode : jwt . decode ,
134
- ...userOptions . jwt ,
122
+ ...userOptions . jwt
135
123
} ,
136
124
// Event messages
137
125
events : {
138
126
...defaultEvents ,
139
- ...userOptions . events ,
127
+ ...userOptions . events
140
128
} ,
141
129
// Callback functions
142
130
callbacks : {
143
131
...defaultCallbacks ,
144
- ...userOptions . callbacks ,
132
+ ...userOptions . callbacks
145
133
} ,
146
134
pkce : { } ,
147
- logger,
135
+ logger
148
136
}
149
137
150
138
csrfTokenHandler ( req , res )
@@ -153,79 +141,65 @@ async function NextAuthHandler(req, res, userOptions) {
153
141
const render = renderPage ( req , res )
154
142
const { pages } = req . options
155
143
156
- if ( req . method === " GET" ) {
144
+ if ( req . method === ' GET' ) {
157
145
switch ( action ) {
158
- case " providers" :
146
+ case ' providers' :
159
147
return routes . providers ( req , res )
160
- case " session" :
148
+ case ' session' :
161
149
return routes . session ( req , res )
162
- case " csrf" :
150
+ case ' csrf' :
163
151
return res . json ( { csrfToken : req . options . csrfToken } )
164
- case " signin" :
152
+ case ' signin' :
165
153
if ( pages . signIn ) {
166
- let signinUrl = `${ pages . signIn } ${
167
- pages . signIn . includes ( "?" ) ? "&" : "?"
168
- } callbackUrl=${ req . options . callbackUrl } `
169
- if ( error ) {
170
- signinUrl = `${ signinUrl } &error=${ error } `
171
- }
154
+ let signinUrl = `${ pages . signIn } ${ pages . signIn . includes ( '?' ) ? '&' : '?' } callbackUrl=${ req . options . callbackUrl } `
155
+ if ( error ) { signinUrl = `${ signinUrl } &error=${ error } ` }
172
156
return res . redirect ( signinUrl )
173
157
}
174
158
175
159
return render . signin ( )
176
- case " signout" :
160
+ case ' signout' :
177
161
if ( pages . signOut ) {
178
- return res . redirect (
179
- `${ pages . signOut } ${
180
- pages . signOut . includes ( "?" ) ? "&" : "?"
181
- } error=${ error } `
182
- )
162
+ return res . redirect ( `${ pages . signOut } ${ pages . signOut . includes ( '?' ) ? '&' : '?' } error=${ error } ` )
183
163
}
184
164
return render . signout ( )
185
- case " callback" :
165
+ case ' callback' :
186
166
if ( provider ) {
187
167
if ( await pkce . handleCallback ( req , res ) ) return
188
168
if ( await state . handleCallback ( req , res ) ) return
189
169
return routes . callback ( req , res )
190
170
}
191
171
break
192
- case " verify-request" :
172
+ case ' verify-request' :
193
173
if ( pages . verifyRequest ) {
194
174
return res . redirect ( pages . verifyRequest )
195
175
}
196
176
return render . verifyRequest ( )
197
- case " error" :
177
+ case ' error' :
198
178
if ( pages . error ) {
199
- return res . redirect (
200
- `${ pages . error } ${
201
- pages . error . includes ( "?" ) ? "&" : "?"
202
- } error=${ error } `
203
- )
179
+ return res . redirect ( `${ pages . error } ${ pages . error . includes ( '?' ) ? '&' : '?' } error=${ error } ` )
204
180
}
205
181
206
182
// These error messages are displayed in line on the sign in page
207
- if (
208
- [
209
- "Signin" ,
210
- "OAuthSignin" ,
211
- "OAuthCallback" ,
212
- "OAuthCreateAccount" ,
213
- "EmailCreateAccount" ,
214
- "Callback" ,
215
- "OAuthAccountNotLinked" ,
216
- "EmailSignin" ,
217
- "CredentialsSignin" ,
218
- ] . includes ( error )
219
- ) {
183
+ if ( [
184
+ 'Signin' ,
185
+ 'OAuthSignin' ,
186
+ 'OAuthCallback' ,
187
+ 'OAuthCreateAccount' ,
188
+ 'EmailCreateAccount' ,
189
+ 'Callback' ,
190
+ 'OAuthAccountNotLinked' ,
191
+ 'EmailSignin' ,
192
+ 'CredentialsSignin'
193
+ ] . includes ( error ) ) {
220
194
return res . redirect ( `${ baseUrl } ${ basePath } /signin?error=${ error } ` )
221
195
}
222
196
223
197
return render . error ( { error } )
224
198
default :
225
199
}
226
- } else if ( req . method === " POST" ) {
200
+ } else if ( req . method === ' POST' ) {
227
201
switch ( action ) {
228
- case " signin" :
202
+ case ' signin' :
229
203
// Verified CSRF Token required for all sign in routes
230
204
if ( req . options . csrfTokenVerified && provider ) {
231
205
if ( await pkce . handleSignin ( req , res ) ) return
@@ -234,19 +208,16 @@ async function NextAuthHandler(req, res, userOptions) {
234
208
}
235
209
236
210
return res . redirect ( `${ baseUrl } ${ basePath } /signin?csrf=true` )
237
- case " signout" :
211
+ case ' signout' :
238
212
// Verified CSRF Token required for signout
239
213
if ( req . options . csrfTokenVerified ) {
240
214
return routes . signout ( req , res )
241
215
}
242
216
return res . redirect ( `${ baseUrl } ${ basePath } /signout?csrf=true` )
243
- case " callback" :
217
+ case ' callback' :
244
218
if ( provider ) {
245
219
// Verified CSRF Token required for credentials providers only
246
- if (
247
- provider . type === "credentials" &&
248
- ! req . options . csrfTokenVerified
249
- ) {
220
+ if ( provider . type === 'credentials' && ! req . options . csrfTokenVerified ) {
250
221
return res . redirect ( `${ baseUrl } ${ basePath } /signin?csrf=true` )
251
222
}
252
223
@@ -255,33 +226,31 @@ async function NextAuthHandler(req, res, userOptions) {
255
226
return routes . callback ( req , res )
256
227
}
257
228
break
258
- case " _log" :
229
+ case ' _log' :
259
230
if ( userOptions . logger ) {
260
231
try {
261
232
const {
262
- code = " CLIENT_ERROR" ,
263
- level = " error" ,
264
- message = "[]" ,
233
+ code = ' CLIENT_ERROR' ,
234
+ level = ' error' ,
235
+ message = '[]'
265
236
} = req . body
266
237
267
238
logger [ level ] ( code , ...JSON . parse ( message ) )
268
239
} catch ( error ) {
269
240
// If logging itself failed...
270
- logger . error ( " LOGGER_ERROR" , error )
241
+ logger . error ( ' LOGGER_ERROR' , error )
271
242
}
272
243
}
273
244
return res . end ( )
274
245
default :
275
246
}
276
247
}
277
- return res
278
- . status ( 400 )
279
- . end ( `Error: HTTP ${ req . method } is not supported for ${ req . url } ` )
248
+ return res . status ( 400 ) . end ( `Error: HTTP ${ req . method } is not supported for ${ req . url } ` )
280
249
} )
281
250
}
282
251
283
252
/** Tha main entry point to next-auth */
284
- export default function NextAuth ( ...args ) {
253
+ export default function NextAuth ( ...args ) {
285
254
if ( args . length === 1 ) {
286
255
return ( req , res ) => NextAuthHandler ( req , res , args [ 0 ] )
287
256
}
0 commit comments