ace
ace

Reputation: 905

Node.js passport skips strategy

This coffee-script passport implementation looks just like the examples to me but fails every time and never prints "Trying out the strategy". I just get redirected to "/fail". I tried naming the strategy as well as executing it in the (req, res, next) handler. I also verified that the form posted sent the username and password in those fields and tried renaming them with a mapping in the strategy according to the examples to no avail. Any tips on what I'm overlooking?

pass = require 'passport'
strat = require('passport-local').Strategy
exp = require 'express'
app = exp.createServer()

# Configure strategy
pass.use new strat (username, password, done) ->
    #Logic to find user
    console.log("Trying out the strategy")
    user = {nm:username,ps:password}
    done(null,user)

app.configure () ->
    app.use (req,res,next) ->
        console.log("GOT A REQ")
        next()
    app.use pass.initialize()

ops = { failureRedirect: '/fail' }
app.post '/auth', pass.authenticate('local',ops), (req, res, next) ->
    console.log "what about here"

app.listen 1337

Solution Modify express configuration:

app.configure () ->
    app.use exp.bodyParser() 

Upvotes: 0

Views: 1810

Answers (3)

ace
ace

Reputation: 905

Turns out this problem was due to my ignorance of express. I was sending the username and password but wasn't parsing it - app.configure requires express.bodyParser() in order to utilize the strategies.

Upvotes: 4

Jared Hanson
Jared Hanson

Reputation: 16000

If your getting redirected to /fail, it suggests to me that Passport is handling the request and authentication isn't successful. Maybe a dumb suggestion, but are you POSTing a form with username and password fields? If either of those are not present, that is the primary failure case in the local strategy.

Upvotes: 1

Ask Bjørn Hansen
Ask Bjørn Hansen

Reputation: 6953

I find the coffee script stuff unbelievably messy to read (maybe post the compiled JS version, too, unless someone who reads the coffee grounds better than I comes by). Anyway, assuming your syntax around calling pass.authenticate is correct (I usually call it with a custom callback where I handle the response) the two things that stands out are:

1) you don't have a passport.serializeUser and deserializeUser setup.

2) unless you call authenticate with session:false and do your own session setup, I think having connect/express sessions loaded and configured is required.

Upvotes: 0

Related Questions