Closed cmoel closed 4 years ago
Hey @cmoel, is that all the code because it seems incomplete. Maybe this is what you want:
import nextConnect from "next-connect"
import passport from "passport"
import {Strategy as LocalStrategy} from "passport-local"
let handler = nextConnect()
let passportInit = passport.initialize()
console.log("testing") // this is successfully written to stdout
// Use the middleware
handler.use(passportInit)
passport.use(
new LocalStrategy(function (username, password, done) {
console.log("the username is", username) // this doesn't get written to stdout
return done(new Error("err"))
})
)
// This handle auth submission
handler.post(passport.authenticate('local'), (req, res) => {
res.json({ user: extractUser(req) });
});
export default handler;
You can look at my setup for passport
here:
Hi @hoangvvo. Thanks for getting back to me.
Yes, my example was incomplete and is similar to what you provided with handler.post
and the default export. Thank you for making sure my example is correct! console.log("the username is", username)
still isn't writing to stdout. Is there something else I'm doing wrong? Or is my expectation that it would write to stdout wrong?
Thank you again!
next-connect would have nothing to do with stdout so it will not be a cause. Still I’d love to look into this but would need a more complete reproduction. I remembered having terrible experience debugging Passport because it did weird things suppressing all the errors.
I also suggest you to look at my setup above in the two links.
@hoangvvo Thanks again for your help. I was able to get my example working.
I started my project from https://github.com/zeit/next.js/tree/canary/examples/with-passport after express was replaced with next-connect. Ultimately, the issue was related to my passport-local strategy setup. I needed to specify the usernameField
option since I want to use email addresses for usernames:
export const localStrategy = new Local.Strategy(
+ {usernameField: "email"},
(email, password, done) => {
return findUser({email, password})
.then((user) => {
done(null, user)
})
.catch((error) => {
done(error)
})
}
)
After doing that, everything started working. It seems that passport was swallowing errors so I would only get Bad Request
as the error response.
Hello,
I'm having trouble writing to
stdout
usingconsole.log
in a middleware and couldn't figure it out with any DuckDuckGoing or Googling. Can you help me out? The basic example I have is this:After starting the server (running
nom run dev
), theconsole.log
line underpassport.initialize()
gets logged tostdout
while theconsole.log("the username is", username)
line doesn't. Any idea what I'm doing wrong and how I can get the middleware functions to write tostdout
?Thanks!