hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.64k stars 65 forks source link

console.log doesn't show up in stdout #47

Closed cmoel closed 4 years ago

cmoel commented 4 years ago

Hello,

I'm having trouble writing to stdout using console.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:

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

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"))
  })
)

After starting the server (running nom run dev), the console.log line under passport.initialize() gets logged to stdout while the console.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 to stdout?

Thanks!

hoangvvo commented 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:

cmoel commented 4 years ago

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!

hoangvvo commented 4 years ago

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.

cmoel commented 4 years ago

@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.