Advertisement
Guest User

Untitled

a guest
Mar 18th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. info: Listening on 127.0.0.1:3000
  2. debug: GET /
  3. debug: Incorrect password
  4. /home/bob/git/authenticate-nodejs-prototype/node_modules/mongodb/lib/utils.js:98
  5. process.nextTick(function() { throw err; });
  6. ^
  7.  
  8. TypeError: req.flash is not a function
  9. at allFailed (/home/bob/git/authenticate-nodejs-prototype/node_modules/passport/lib/middleware/authenticate.js:118:15)
  10.  
  11. var express = require('express'),
  12. app = express(),
  13. http = require('http').Server(app),
  14. winston = require('winston'),
  15. passport = require('passport'),
  16. LocalStrategy = require('passport-local').Strategy,
  17. ipaddress = '127.0.0.1',
  18. port = 3000,
  19. MongoClient = require('mongodb').MongoClient,
  20. ObjectId = require('mongodb').ObjectID,
  21. assert = require('assert'),
  22. mongoUrl = 'mongodb://' + ipaddress + ':27017/authenticate-nodejs-prototype';
  23.  
  24. // during dev
  25. winston.level = 'debug';
  26.  
  27.  
  28. /*
  29. * Database query
  30. * Searches db for user that matches provided username
  31. */
  32. var findUser = function (db, id, callback) {
  33. var cursor = db.collection('userInfo').find({username: id.username});
  34. var result = [];
  35. cursor.each(function (err, doc) {
  36. assert.equal(err, null);
  37. if (doc !== null) {
  38. result.push(doc);
  39. } else {
  40. callback(result);
  41. }
  42. });
  43. };
  44.  
  45.  
  46. // configure passport to use username and password authentication
  47. passport.use(new LocalStrategy(
  48. function(username, password, done) {
  49. MongoClient.connect(mongoUrl, function (err, db) {
  50. assert.equal(null, err);
  51. findUser(db, {username:username, password:password}, function (result) {
  52. db.close();
  53.  
  54. if (err) {
  55. return done(err);
  56. }else if (result === []) {
  57. winston.debug('Incorrect username');
  58. return done(null, false, { message: 'Incorrect username.' });
  59. }else if (password !== result.password) {
  60. winston.debug('Incorrect password');
  61. return done(null, false, { message: 'Incorrect password.' }); // this is the line that causes error
  62. }
  63. return done(null, result);
  64. });
  65. });
  66. }
  67. ));
  68. passport.serializeUser(function(user, done) {
  69. return done(null, user);
  70. });
  71. passport.deserializeUser(function(id, done) {
  72. MongoClient.connect(mongoUrl, function (err, db) {
  73. assert.equal(null, err);
  74. findUser(db, id, function (result) {
  75. db.close();
  76. return done(null, result);
  77. });
  78. });
  79. });
  80.  
  81. app.configure(function() {
  82. app.use(express.static('public'));
  83. app.use(express.cookieParser());
  84. app.use(express.bodyParser());
  85. app.use(express.session({ secret: 'keyboard cat' }));
  86. app.use(passport.initialize());
  87. app.use(passport.session());
  88. app.use(app.router);
  89. });
  90.  
  91. /*
  92. * setup endpoints
  93. */
  94. app.get('/', function(req, res){
  95. winston.debug('GET /');
  96. res.sendfile('views/index.html');
  97. });
  98. app.get('/success', function(req, res){
  99. winston.debug('GET /success');
  100. res.sendfile('views/success.html');
  101. });
  102. app.post('/login',
  103. passport.authenticate('local', { successRedirect: '/success',
  104. failureRedirect: '/',
  105. failureFlash: true })
  106. );
  107.  
  108. // start server
  109. http.listen(port, ipaddress, function(){
  110. winston.info('Listening on ' + ipaddress + ':' + port);
  111. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement