Skip to content

Commit 3524264

Browse files
committed
chore: revert unnecessary file formatting
1 parent c1be7af commit 3524264

File tree

1 file changed

+72
-103
lines changed

1 file changed

+72
-103
lines changed

src/server/index.js

+72-103
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
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'
1717

1818
// To work properly in production with OAuth providers the NEXTAUTH_URL
1919
// environment variable must be set.
2020
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')
2222
}
2323

2424
/**
2525
* @param {import("next").NextApiRequest} req
2626
* @param {import("next").NextApiResponse} res
2727
* @param {import("types").NextAuthOptions} userOptions
2828
*/
29-
async function NextAuthHandler(req, res, userOptions) {
29+
async function NextAuthHandler (req, res, userOptions) {
3030
if (userOptions.logger) {
3131
setLogger(userOptions.logger)
3232
}
@@ -39,54 +39,44 @@ async function NextAuthHandler(req, res, userOptions) {
3939
// to avoid early termination of calls to the serverless function
4040
// (and then return that promise when we are done) - eslint
4141
// 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
4443
extendRes(req, res, resolve)
4544

4645
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.'
4947

50-
logger.error("MISSING_NEXTAUTH_API_ROUTE_ERROR", error)
48+
logger.error('MISSING_NEXTAUTH_API_ROUTE_ERROR', error)
5149
return res.status(500).end(`Error: ${error}`)
5250
}
5351

5452
const {
5553
nextauth,
5654
action = nextauth[0],
5755
providerId = nextauth[1],
58-
error = nextauth[1],
56+
error = nextauth[1]
5957
} = req.query
6058

6159
// @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)
6561

6662
const cookies = {
67-
...cookie.defaultCookies(
68-
userOptions.useSecureCookies || baseUrl.startsWith("https://")
69-
),
63+
...cookie.defaultCookies(userOptions.useSecureCookies || baseUrl.startsWith('https://')),
7064
// Allow user cookie options to override any cookie settings above
71-
...userOptions.cookies,
65+
...userOptions.cookies
7266
}
7367

7468
const secret = createSecret({ userOptions, basePath, baseUrl })
7569

76-
const providers = parseProviders({
77-
providers: userOptions.providers,
78-
baseUrl,
79-
basePath,
80-
})
70+
const providers = parseProviders({ providers: userOptions.providers, baseUrl, basePath })
8171
const provider = providers.find(({ id }) => id === providerId)
8272

8373
// 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')) {
8575
// When provider.state is undefined, we still want this to pass
8676
if (!provider.protection && provider.state !== false) {
8777
// 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') {
9080
provider.protection = [provider.protection]
9181
}
9282
}
@@ -96,16 +86,14 @@ async function NextAuthHandler(req, res, userOptions) {
9686
// Parse database / adapter
9787
// If adapter is provided, use it (advanced usage, overrides database)
9888
// 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))
10290

10391
// User provided options are overriden by other options,
10492
// except for the options with special handling above
10593
req.options = {
10694
debug: false,
10795
pages: {},
108-
theme: "auto",
96+
theme: 'auto',
10997
// Custom options override defaults
11098
...userOptions,
11199
// These computed settings can have values in userOptions but we override them
@@ -123,28 +111,28 @@ async function NextAuthHandler(req, res, userOptions) {
123111
jwt: !adapter, // If no adapter specified, force use of JSON Web Tokens (stateless)
124112
maxAge,
125113
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
127115
},
128116
// JWT options
129117
jwt: {
130118
secret, // Use application secret if no keys specified
131119
maxAge, // same as session maxAge,
132120
encode: jwt.encode,
133121
decode: jwt.decode,
134-
...userOptions.jwt,
122+
...userOptions.jwt
135123
},
136124
// Event messages
137125
events: {
138126
...defaultEvents,
139-
...userOptions.events,
127+
...userOptions.events
140128
},
141129
// Callback functions
142130
callbacks: {
143131
...defaultCallbacks,
144-
...userOptions.callbacks,
132+
...userOptions.callbacks
145133
},
146134
pkce: {},
147-
logger,
135+
logger
148136
}
149137

150138
csrfTokenHandler(req, res)
@@ -153,79 +141,65 @@ async function NextAuthHandler(req, res, userOptions) {
153141
const render = renderPage(req, res)
154142
const { pages } = req.options
155143

156-
if (req.method === "GET") {
144+
if (req.method === 'GET') {
157145
switch (action) {
158-
case "providers":
146+
case 'providers':
159147
return routes.providers(req, res)
160-
case "session":
148+
case 'session':
161149
return routes.session(req, res)
162-
case "csrf":
150+
case 'csrf':
163151
return res.json({ csrfToken: req.options.csrfToken })
164-
case "signin":
152+
case 'signin':
165153
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}` }
172156
return res.redirect(signinUrl)
173157
}
174158

175159
return render.signin()
176-
case "signout":
160+
case 'signout':
177161
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}`)
183163
}
184164
return render.signout()
185-
case "callback":
165+
case 'callback':
186166
if (provider) {
187167
if (await pkce.handleCallback(req, res)) return
188168
if (await state.handleCallback(req, res)) return
189169
return routes.callback(req, res)
190170
}
191171
break
192-
case "verify-request":
172+
case 'verify-request':
193173
if (pages.verifyRequest) {
194174
return res.redirect(pages.verifyRequest)
195175
}
196176
return render.verifyRequest()
197-
case "error":
177+
case 'error':
198178
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}`)
204180
}
205181

206182
// 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)) {
220194
return res.redirect(`${baseUrl}${basePath}/signin?error=${error}`)
221195
}
222196

223197
return render.error({ error })
224198
default:
225199
}
226-
} else if (req.method === "POST") {
200+
} else if (req.method === 'POST') {
227201
switch (action) {
228-
case "signin":
202+
case 'signin':
229203
// Verified CSRF Token required for all sign in routes
230204
if (req.options.csrfTokenVerified && provider) {
231205
if (await pkce.handleSignin(req, res)) return
@@ -234,19 +208,16 @@ async function NextAuthHandler(req, res, userOptions) {
234208
}
235209

236210
return res.redirect(`${baseUrl}${basePath}/signin?csrf=true`)
237-
case "signout":
211+
case 'signout':
238212
// Verified CSRF Token required for signout
239213
if (req.options.csrfTokenVerified) {
240214
return routes.signout(req, res)
241215
}
242216
return res.redirect(`${baseUrl}${basePath}/signout?csrf=true`)
243-
case "callback":
217+
case 'callback':
244218
if (provider) {
245219
// 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) {
250221
return res.redirect(`${baseUrl}${basePath}/signin?csrf=true`)
251222
}
252223

@@ -255,33 +226,31 @@ async function NextAuthHandler(req, res, userOptions) {
255226
return routes.callback(req, res)
256227
}
257228
break
258-
case "_log":
229+
case '_log':
259230
if (userOptions.logger) {
260231
try {
261232
const {
262-
code = "CLIENT_ERROR",
263-
level = "error",
264-
message = "[]",
233+
code = 'CLIENT_ERROR',
234+
level = 'error',
235+
message = '[]'
265236
} = req.body
266237

267238
logger[level](code, ...JSON.parse(message))
268239
} catch (error) {
269240
// If logging itself failed...
270-
logger.error("LOGGER_ERROR", error)
241+
logger.error('LOGGER_ERROR', error)
271242
}
272243
}
273244
return res.end()
274245
default:
275246
}
276247
}
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}`)
280249
})
281250
}
282251

283252
/** Tha main entry point to next-auth */
284-
export default function NextAuth(...args) {
253+
export default function NextAuth (...args) {
285254
if (args.length === 1) {
286255
return (req, res) => NextAuthHandler(req, res, args[0])
287256
}

0 commit comments

Comments
 (0)