title: Node.js user authentication using passport
body: <p>(updated code with serialization functions - still redirects to /failedRedirect)</p>
  
  <p>I'm trying to get simple username/password authentication going using the passport package, but failing. In the example below I've tried to verify that authentication works by basically always returning a valid authentication (regardless of what gets passed), but for some reason it fails and passport redirects to the failed login link.</p>
  
  <p>If anybody could help me out in figuring out how to get this example to simply authenticate anything, I should be able to manage from there.</p>
  
  <p>The code in coffeescript is:</p>
  
  <pre class="lang-coffeescript prettyprint-override"><code>express = require "express"
  passport = require "passport"
  LocalStrategy = require("passport-local").Strategy
  
  passport.use(new LocalStrategy( (username, password, done) -&gt;
    console.log "LocalStrategy invoked"
    done(null, {id: 1, name: "Marius"})
  ))
  
  passport.serializeUser (user, done) -&gt;
    done null, user
  
  passport.deserializeUser (obj, done) -&gt;
    done null, obj
  
  app = express.createServer()
  
  app.configure -&gt;
    app.use express.bodyParser()
    app.use express.static("./public")
    app.use express.cookieParser("SOMESECRET")
    app.use express.session
      secret: "SOMESECRET"
      cookie:
        maxAge: 60000
    app.use passport.initialize()
    app.use passport.session()
    app.set "view", "./srv/views"
    app.set "view engine", "jade"
  
  app.get "/login", (req, res) -&gt;
    res.send "login page"
  
  app.post "/login", passport.authenticate("local",
    failureRedirect: "/failedRedirect"
    successRedirect: "/successRedirect"
    failureFlash: true)
  
  app.listen 8082
  </code></pre>
  
  <p><b>Solved:</b> Ok, I believe there were a few reasons why I could not get it working. The serialize stuff may be one (I haven't tested), but since Jared said they were needed, I'm leaving them in (he's the author of Passport). The other confusion may be related to express versions and my confusion with npm. I believe I tested both the latest v2 of express, but I've also tested v3, which I am running now. For version three, you probably should check out the <code>connect-flash</code> module on Github as well, as some to the "flash" stuff which is used in Jared's examples was moved out of express v3 (so the module puts it back in...). And finally, make sure you post using the proper named input names (<code>username</code> and <code>password</code> by default).</p>