Skip to content

Instantly share code, notes, and snippets.

@switzer
Created January 22, 2013 03:38
Show Gist options
  • Save switzer/4591861 to your computer and use it in GitHub Desktop.
Save switzer/4591861 to your computer and use it in GitHub Desktop.
Cannot get second Passport authentication (when not calling /login) to work.
$ curl -i --data "[email protected]&password=password" http://localhost:3000/login
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 260
Set-Cookie: connect.sid=s%3A9b4c4dMSkSiSEex%2BOgtsXBsu.BUoXyrHk95nvhAfDY3mkOIuk0I8npOhdAzMCc4WHnsA; Path=/; HttpOnly
Date: Tue, 22 Jan 2013 03:32:13 GMT
Connection: keep-alive
{
"_id": "50f97e0b96d98bf0af000002",
"email": "[email protected]",
"name": {
"last": "User",
"first": "Joe"
},
"language": "en",
"is_email_confirmed": true,
"created": "2013-01-18T16:53:31.068Z",
"modified": "2013-01-18T16:54:38.368Z"
}
$ curl -i b "connect.sid=s%3A9b4c4dMSkSiSEex%2BOgtsXBsu.BUoXyrHk95nvhAfDY3mkOIuk0I8npOhdAzMCc4WHnsA" http://localhost:3000/accounts/50f97e0b96d98bf0af000002
HTTP/1.1 401 Unauthorized
X-Powered-By: Express
Date: Tue, 22 Jan 2013 03:32:57 GMT
Connection: keep-alive
Transfer-Encoding: chunked
var config = require('./config')
, express = require("express")
, path = require("path")
, http = require("http")
, mongoose = require("mongoose")
, errorHandler = require("./lib/errors")
, passport = require("passport")
, LocalStrategy = require("passport-local").Strategy
, user = require("./lib/users/service")
, User = user.User;
var app = exports.app = express();
app.configure(function () {
app.set("db_url", config.db[app.settings.env]);
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({secret: "keyboard cat"}));
app.use(passport.initialize());
app.use(passport.session());
app.use(errorHandler);
app.use(app.router);
});
mongoose.connect(app.get("db_url"));
passport.use(new LocalStrategy(
{usernameField: 'email'},
function(username, password, done) {
User.findOne({ email: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Email address not found.' }); }
User.comparePasswordAndHash(password, user.password_hash, function(err, valid) {
if (err) { return done(err) };
if (!valid) { return done(null, false, {message: "Incorrect password"})};
return done(null, user);
});
});
}
));
passport.serializeUser(function (user, done) {
done(null, user._id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
console.log("err: " + JSON.stringify(err) + ", user: " + JSON.stringify(user));
done(err, user);
});
});
app.post("/login", passport.authenticate("local"), user.show);
app.get("/accounts/:userid", passport.authenticate("local"), user.show);
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
@switzer
Copy link
Author

switzer commented Jan 22, 2013

Note that in the /accounts/:userid route, user.show never gets called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment