Thursday, 15 April 2010

javascript - Node, Express 3 and Passport: Login form not working -



javascript - Node, Express 3 and Passport: Login form not working -

i new node , have node, express , passport setup , seemingly working. is, see no error messages.

the problem when go login page , seek login, nil happens. page refreshes after click button no action has been taken.

i've pasted code below.

here's terminal output when submit form:

08:19:53 web.1 | post /login 302 3ms - 68 08:19:53 web.1 | /login 304 11ms 08:19:53 web.1 | /stylesheets/base.css 304 1ms

here's login view (jade):

- if (message) p strong =message form(method="post", action="/login") fieldset label(for="username") username input.text-input(type="text", name="username") label(for="password") password input.text-input(type="password", name="password") button.btn-submit(type="submit") login! a.btn-cancel(href="/") cancel

here's code calls login page (i took out other stuff sake of clarity):

app.js

app.get("/login", routes.login);

routes/index.js

(function() { var post; post = require('../models/post'); module.exports = { login: function(req, res) { homecoming res.render("login", { user: req.user, message: req.flash("login error!"), title: "login | david yeiser" }); } }; }).call(this);

and here's part of app.js handles post:

app.post("/login", passport.authenticate("local", { failureredirect: "/login", failureflash: true }), function(req, res) { homecoming res.redirect("/office"); });

lastly, here's entire app.js:

(function() { var localstrategy, app, express, findbyid, findbyusername, flash, http, mongoose, passport, path, routes, user, users, util; express = require('express'); routes = require('./routes'); user = require('./routes/user'); http = require('http'); path = require('path'); mongoose = require('mongoose'); passport = require('passport'); flash = require('connect-flash'); util = require('util'); localstrategy = require('passport-local').strategy; users = [ { id: 1, username: 'bob', password: 'secret', email: 'bob@me.com', id: 2, username: 'nancy', password: 'secrettoo', email: 'nancy@me.com' } ]; findbyid = function(id, fn) { var idx; idx = id - 1; if (users[idx]) { homecoming fn(null, users[idx]); } else { homecoming fn(new error('user not exist')); } }; findbyusername = function(username, fn) { var i, len; = 0; len = users.length; while (i < len) { user = users[i]; if (user.username === username) { homecoming fn(null, user); } i++; } homecoming fn(null, null); }; passport.serializeuser(function(user, done) { homecoming done(null, user.id); }); passport.deserializeuser(function(id, done) { homecoming findbyid(id, function(err, user) { homecoming done(err, user); }); }); passport.use(new localstrategy(function(username, password, done) { homecoming process.nexttick(function() { homecoming findbyusername(username, function(err, user) { if (err) { homecoming done(err); } if (!user) { homecoming done(null, false, { message: "unknown user " + username }); } if (user.password !== password) { homecoming done(null, false, { message: "invalid password" }); } homecoming done(null, user); }); }); })); app = express(); app.configure(function() { app.set('port', process.env.port || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.cookieparser()); app.use(express.bodyparser()); app.use(express.methodoverride()); app.use(express.session({ secret: 'keyboard cat' })); app.use(flash()); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); homecoming app.use(express["static"](path.join(__dirname, 'public'))); }); app.configure('development', function() { mongoose.connect('mongodb://localhost/davidyeiser_dev'); homecoming app.use(express.errorhandler()); }); app.get("/", routes.index); app.get("/login", routes.login); app.post("/login", passport.authenticate("local", { failureredirect: "/login", failureflash: true }), function(req, res) { homecoming res.redirect("/office"); }); app.get("/logout", function(req, res) { req.logout(); homecoming res.redirect("/"); }); app.get("/post/new", routes.newpost); app.post("/post/new", routes.addpost); app.get("/post/:id", routes.viewpost); app.get("/users", user.list); http.createserver(app).listen(app.get("port"), function() { homecoming console.log("express server listening on port " + app.get("port")); }); }).call(this);

most of see taken passport express 3 example: https://github.com/jaredhanson/passport-local/blob/master/examples/express3/

i'm thankful help can give me.

aha! users array wasn't constructed properly.

users = [ { id: 1, username: 'bob', password: 'secret', email: 'bob@me.com', id: 2, username: 'nancy', password: 'secrettoo', email: 'nancy@me.com' } ];

needed be:

users = [ { id: 1, username: 'bob', password: 'secret', email: 'bob@me.com' }, { id: 2, username: 'nancy', password: 'secrettoo', email: 'nancy@me.com' } ];

the way found reduced 1 user (in midst of 1,000 other things) realized error. else remains unchanged.

(the reason didn't grab before because using "bob" every time, , never thought seek "nancy")

javascript node.js express passport.js

No comments:

Post a Comment