I am following an ebook tutorial where I got stuck in a piece of code. The code is supposed to take username and password in JSON as data throough Insomnia or Postman and should return a login success cookie. But my API when testing with Insomnia, returns Unauthorized
. [Image Attached]
Code:
passport.use(
new Strategy(function (username, password, cb) {
const isAdmin = (username === 'admin') && (password === adminPassword)
if (isAdmin) cb(null, { username: 'admin' })
cb(null, false)
})
)
passport.serializeUser((user, cb) => cb(null, user))
passport.deserializeUser((user, cb) => cb(null, user))
app.use(
expressSession({
secret: sessionSecret,
resave: false,
saveUninitialized: false
})
)
app.use(passport.initialize())
app.use(passport.session())
app.post('/login', passport.authenticate('local'), (req, res) =>
res.json({ success: true })
)
Problem Traceback:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header (G:\Work\node\01\node_modules\express\lib\response.js:794:10)
at ServerResponse.json (G:\Work\node\01\node_modules\express\lib\response.js:275:10)
at G:\Work\node\01\complete-servert\server-01.js:44:9
at Layer.handle [as handle_request] (G:\Work\node\01\node_modules\express\lib\router\layer.js:95:5)
at next (G:\Work\node\01\node_modules\express\lib\router\route.js:144:13)
at complete (G:\Work\node\01\node_modules\passport\lib\middleware\authenticate.js:271:13)
at G:\Work\node\01\node_modules\passport\lib\middleware\authenticate.js:278:15
at pass (G:\Work\node\01\node_modules\passport\lib\authenticator.js:428:14)
at Authenticator.transformAuthInfo (G:\Work\node\01\node_modules\passport\lib\authenticator.js:450:5) {
code: 'ERR_HTTP_HEADERS_SENT'
}
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:485:11)
at ServerResponse.header (G:\Work\node\01\node_modules\express\lib\response.js:794:10)
at ServerResponse.json (G:\Work\node\01\node_modules\express\lib\response.js:275:10)
at handleError (G:\Work\node\01\complete-servert\middleware.js:26:21)
at Layer.handle_error (G:\Work\node\01\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (G:\Work\node\01\node_modules\express\lib\router\index.js:326:13)
at G:\Work\node\01\node_modules\express\lib\router\index.js:286:9
at Function.process_params (G:\Work\node\01\node_modules\express\lib\router\index.js:346:12)
at next (G:\Work\node\01\node_modules\express\lib\router\index.js:280:10)
at next (G:\Work\node\01\node_modules\express\lib\router\route.js:129:14)
Edit: added strategy code
CodePudding user response:
You are calling the authentication strategy callback twice. Use an else statement or add a return to make sure you only call one of the callbacks:
passport.use(
new Strategy(function (username, password, cb) {
const isAdmin = (username === 'admin') && (password === adminPassword)
if (isAdmin) return cb(null, { username: 'admin' })
cb(null, false)
})
)