Skip to content

Commit

Permalink
fix: don't chain on res.end on non-chainable res methods (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Feb 1, 2021
1 parent 8e87137 commit 9ed75c7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
21 changes: 14 additions & 7 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ async function NextAuthHandler (req, res, userSuppliedOptions) {
const error = 'Cannot find [...nextauth].js in pages/api/auth. Make sure the filename is written correctly.'

logger.error('MISSING_NEXTAUTH_API_ROUTE_ERROR', error)
return res.status(500).end(`Error: ${error}`).end()
res.status(500)
return res.end(`Error: ${error}`)
}

const { url, query, body } = req
Expand Down Expand Up @@ -232,7 +233,8 @@ async function NextAuthHandler (req, res, userSuppliedOptions) {
session(req, res)
break
case 'csrf':
return res.json({ csrfToken }).end()
res.json({ csrfToken })
return res.end()
case 'signin':
if (options.pages.signIn) {
let redirectUrl = `${options.pages.signIn}${options.pages.signIn.includes('?') ? '&' : '?'}callbackUrl=${callbackUrl}`
Expand All @@ -253,7 +255,8 @@ async function NextAuthHandler (req, res, userSuppliedOptions) {
if (provider && options.providers[provider]) {
callback(req, res)
} else {
return res.status(400).end(`Error: HTTP GET is not supported for ${url}`).end()
res.status(400)
return res.end(`Error: HTTP GET is not supported for ${url}`)
}
break
case 'verify-request':
Expand All @@ -267,7 +270,8 @@ async function NextAuthHandler (req, res, userSuppliedOptions) {
renderPage(req, res, 'error', { error })
break
default:
return res.status(404).end()
res.status(404)
return res.end()
}
} else if (req.method === 'POST') {
switch (action) {
Expand Down Expand Up @@ -298,14 +302,17 @@ async function NextAuthHandler (req, res, userSuppliedOptions) {

callback(req, res)
} else {
return res.status(400).end(`Error: HTTP POST is not supported for ${url}`).end()
res.status(400)
return res.end(`Error: HTTP POST is not supported for ${url}`)
}
break
default:
return res.status(400).end(`Error: HTTP POST is not supported for ${url}`).end()
res.status(400)
return res.end(`Error: HTTP POST is not supported for ${url}`)
}
} else {
return res.status(400).end(`Error: HTTP ${req.method} is not supported for ${url}`).end()
res.status(400)
return res.end(`Error: HTTP ${req.method} is not supported for ${url}`)
}
})
}
Expand Down
7 changes: 3 additions & 4 deletions src/server/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export default function renderPage (req, res, page, props = {}) {
return
}

res
.setHeader('Content-Type', 'text/html')
.send(`<!DOCTYPE html><head><style type="text/css">${css()}</style><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><div class="page">${html}</div></body></html>`)
.end()
res.setHeader('Content-Type', 'text/html')
res.send(`<!DOCTYPE html><head><style type="text/css">${css()}</style><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><div class="page">${html}</div></body></html>`)
res.end()
}
4 changes: 3 additions & 1 deletion src/server/routes/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ export default function providers (req, res) {
}
}), {})

return res.setHeader('Content-Type', 'application/json').json(result).end()
res.setHeader('Content-Type', 'application/json')
res.json(result)
return res.end()
}
8 changes: 6 additions & 2 deletions src/server/routes/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export default async function session (req, res) {
const sessionToken = req.cookies[cookies.sessionToken.name]

if (!sessionToken) {
return res.setHeader('Content-Type', 'application/json').json({}).end()
res.setHeader('Content-Type', 'application/json')
res.json({})
return res.end()
}

let response = {}
Expand Down Expand Up @@ -99,5 +101,7 @@ export default async function session (req, res) {
}
}

return res.setHeader('Content-Type', 'application/json').json(response).end()
res.setHeader('Content-Type', 'application/json')
res.json(response)
return res.end()
}
3 changes: 2 additions & 1 deletion src/server/routes/signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default async function signin (req, res) {
const { type } = provider

if (!type) {
return res.status(500).end(`Error: Type not specified for ${provider}`)
res.status(500)
return res.end(`Error: Type not specified for ${provider}`)
}

if (type === 'oauth' && req.method === 'POST') {
Expand Down

0 comments on commit 9ed75c7

Please sign in to comment.