Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. Object #<IncomingMessage> has no method 'authenticate'
  2.  
  3. var express = require('express');
  4. var router = express.Router();
  5.  
  6. var isAuthenticated = function (req, res, next) {
  7. // if user is authenticated in the session, call the next() to call the next request handler
  8. // Passport adds this method to request object. A middleware is allowed to add properties to
  9. // request and response objects
  10. if (req.isAuthenticated())
  11. return next();
  12. // if the user is not authenticated then redirect him to the login page
  13. res.redirect('/');
  14. }
  15.  
  16. module.exports = function(passport){
  17.  
  18. /* GET login page. */
  19. router.get('/', function(req, res) {
  20. // Display the Login page with any flash message, if any
  21. res.render('index', { message: req.flash('message') });
  22. });
  23. ........and so on
  24.  
  25. <!DOCTYPE html>
  26. <html>
  27. <head>
  28. <title></title>
  29. <link rel='stylesheet' href='/stylesheets/style.css' />
  30. </head>
  31. <body>
  32.  
  33.  
  34. <form action="/login" method="post">
  35. <input type="text" name="username" placeholder="username" required autofocus>
  36. <input type="password" name="password" placeholder="password" required>
  37.  
  38. <input type="submit" value="Login">
  39. </form>
  40.  
  41.  
  42.  
  43. </body>
  44. </html>
  45.  
  46. var LocalStrategy = require('passport-local').Strategy;
  47. var User = require('../models/user');
  48. var bCrypt = require('bcrypt-nodejs');
  49.  
  50. module.exports = function(passport){
  51.  
  52. passport.use('login', new LocalStrategy({
  53. passReqToCallback : true
  54. },
  55. function(req, username, password, done) {
  56. // check in mongo if a user with username exists or not
  57. User.findOne({ 'username' : username },
  58. function(err, user) {
  59. // In case of any error, return using the done method
  60. if (err)
  61. return done(err);
  62. // Username does not exist, log the error and redirect back
  63. if (!user){
  64. console.log('User Not Found with username '+username);
  65. return done(null, false, req.flash('message', 'User Not found.'));
  66. }
  67. // User exists but wrong password, log the error
  68. if (!isValidPassword(user, password)){
  69. console.log('Invalid Password');
  70. return done(null, false, req.flash('message', 'Invalid Password')); // redirect back to login page
  71. }
  72. // User and password both match, return user from done method
  73. // which will be treated like success
  74. return done(null, user);
  75. }
  76. );
  77.  
  78. })
  79. );
  80.  
  81.  
  82. var isValidPassword = function(user, password){
  83. return bCrypt.compareSync(password, user.password);
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement