Unverified Commit 20f64296 authored by Luis Alvarez D's avatar Luis Alvarez D Committed by GitHub

Replaced express with next-connect (#11747)

parent ee372f47
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
"dependencies": { "dependencies": {
"@hapi/iron": "6.0.0", "@hapi/iron": "6.0.0",
"cookie": "0.4.0", "cookie": "0.4.0",
"express": "4.17.1",
"next": "latest", "next": "latest",
"next-connect": "0.6.1",
"passport": "0.4.1", "passport": "0.4.1",
"passport-local": "1.0.0", "passport-local": "1.0.0",
"react": "latest", "react": "latest",
......
import express from 'express'
import passport from 'passport' import passport from 'passport'
import nextConnect from 'next-connect'
import { localStrategy } from '../../lib/password-local' import { localStrategy } from '../../lib/password-local'
import { encryptSession } from '../../lib/iron' import { encryptSession } from '../../lib/iron'
import { setTokenCookie } from '../../lib/auth-cookies' import { setTokenCookie } from '../../lib/auth-cookies'
const app = express()
const authenticate = (method, req, res) => const authenticate = (method, req, res) =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
passport.authenticate(method, { session: false }, (error, token) => { passport.authenticate(method, { session: false }, (error, token) => {
...@@ -16,26 +15,22 @@ const authenticate = (method, req, res) => ...@@ -16,26 +15,22 @@ const authenticate = (method, req, res) =>
})(req, res) })(req, res)
}) })
app.disable('x-powered-by')
app.use(passport.initialize())
passport.use(localStrategy) passport.use(localStrategy)
app.post('/api/login', async (req, res) => { export default nextConnect()
try { .use(passport.initialize())
const user = await authenticate('local', req, res) .post(async (req, res) => {
// session is the payload to save in the token, it may contain basic info about the user try {
const session = { ...user } const user = await authenticate('local', req, res)
// The token is a string with the encrypted session // session is the payload to save in the token, it may contain basic info about the user
const token = await encryptSession(session) const session = { ...user }
// The token is a string with the encrypted session
const token = await encryptSession(session)
setTokenCookie(res, token) setTokenCookie(res, token)
res.status(200).send({ done: true }) res.status(200).send({ done: true })
} catch (error) { } catch (error) {
console.error(error) console.error(error)
res.status(401).send(error.message) res.status(401).send(error.message)
} }
}) })
export default app
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment