Skip to content

Commit

Permalink
Replaced express with next-connect (#11747)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Alvarez D authored Apr 13, 2020
1 parent ee372f4 commit 20f6429
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/with-passport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"dependencies": {
"@hapi/iron": "6.0.0",
"cookie": "0.4.0",
"express": "4.17.1",
"next": "latest",
"next-connect": "0.6.1",
"passport": "0.4.1",
"passport-local": "1.0.0",
"react": "latest",
Expand Down
39 changes: 17 additions & 22 deletions examples/with-passport/pages/api/login.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import express from 'express'
import passport from 'passport'
import nextConnect from 'next-connect'
import { localStrategy } from '../../lib/password-local'
import { encryptSession } from '../../lib/iron'
import { setTokenCookie } from '../../lib/auth-cookies'

const app = express()
const authenticate = (method, req, res) =>
new Promise((resolve, reject) => {
passport.authenticate(method, { session: false }, (error, token) => {
Expand All @@ -16,26 +15,22 @@ const authenticate = (method, req, res) =>
})(req, res)
})

app.disable('x-powered-by')

app.use(passport.initialize())

passport.use(localStrategy)

app.post('/api/login', async (req, res) => {
try {
const user = await authenticate('local', req, res)
// session is the payload to save in the token, it may contain basic info about the user
const session = { ...user }
// The token is a string with the encrypted session
const token = await encryptSession(session)
export default nextConnect()
.use(passport.initialize())
.post(async (req, res) => {
try {
const user = await authenticate('local', req, res)
// session is the payload to save in the token, it may contain basic info about the user
const session = { ...user }
// The token is a string with the encrypted session
const token = await encryptSession(session)

setTokenCookie(res, token)
res.status(200).send({ done: true })
} catch (error) {
console.error(error)
res.status(401).send(error.message)
}
})

export default app
setTokenCookie(res, token)
res.status(200).send({ done: true })
} catch (error) {
console.error(error)
res.status(401).send(error.message)
}
})

0 comments on commit 20f6429

Please sign in to comment.