2017-06-23 97 views
1

我正在關注Udemy教程之一,並且我陷入了登錄過程的最後一點。我能夠註冊新用戶但無法登錄。我輸入電子郵件地址和密碼。按提交按鈕不管電子郵件地址或密碼的值是正確還是錯誤,都會觸發failureRedirect再次拉登錄頁的護照方法,我已經放了幾個console.log,但是沒有一個打印在控制檯中。同樣,如果輸入任何隨機細節,它應該拋出未知的用戶錯誤。我不確定是什麼導致它無法正常工作,沒有在控制檯或瀏覽器中看到或發現錯誤。無法使用護照登錄 - Nodejs

形式 - login.ejs

<form class="form-signin" action="/login" method="POST"> 
     <h2 class="form-signin-heading">Please sign in</h2> 
     <label for="inputEmail" class="sr-only">Email address</label> 
     <input type="email" id="email" name="email" class="form-control" placeholder="Email address" required autofocus> 
     <label for="inputPassword" class="sr-only">Password</label> 
     <input type="password" id="password" name="password" class="form-control" placeholder="Password" required> 
     <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 
</form> 

路線 - index.js

var express = require('express'); 
var router = express.Router(); 
var passport = require('passport'); 
var LocalStrategy = require('passport-local').Strategy; 

var User = require('../models/user'); 
router.get('/login', function(req, res, next) { 
    res.render('login', { title: 'login', errors: false }); 
}); 


passport.serializeUser(function(user, done) { 
    done(null, user.id); 
}); 

passport.deserializeUser(function(id, done) { 
    User.getUserById(id, function(err, user) { 
    done(err, user); 
    }); 
}); 

passport.use(new LocalStrategy(
    function (email, password, done){ 
     console.log("testing... 1"); 
     User.getUserByEmail(email, function(err, user){ 
      if (err) throw err; 
      if(!user){ 
       console.log('Unknown User'); 
       console.log("testing... 3"); 
       return done(null, false, {message: 'Unknown User'}); 
      } 

      User.comparePassword(password, user.password, function(err, isMatch){ 
       console.log("testing... 2"); 
       if(err) throw err; 
       if (isMatch){ 
        return done(null, user); 
        console.log("testing... 4"); 
       }else{ 
        console.log("Invalid Password"); 
        return done(null,false, {message:'Invalid Password'}); 
       } 
      }); 
     }); 
    } 
)); 


router.post('/login', passport.authenticate('local', {failureRedirect: '/login', failureFlash: 'Invalid username or password'}), function(req, res) { 

    console.log('Authentication successful'); 
    //Flash message 
    req.flash('success','Your credentials match redirecting...'); 
    //Redirect to the dashboard page 
    res.location('/dashboard'); 
    res.redirect ('/dashboard'); 

}); 

模型 - user.js的

var mongoose = require('mongoose'); 
var bcrypt = require('bcrypt'); 
mongoose.connect('mongodb://localhost/db_name'); 
var db = mongoose.connection; 

//User Schema 

var UserSchema = mongoose.Schema({ 
    name:{ 
     type: String, 
     index:true 
    }, 
    email:{ 
     type:String 
    }, 
    password:{ 
     type:String, required:true, bcrypt:true 
    } 
}); 

var User = module.exports = mongoose.model('User', UserSchema); 

module.exports.comparePassword = function(candidatePassword, hash, callback){ 
    bcrypt.compare(candidatePassword, hash, function(err, isMatch){ 
     if(err) return callback(err); 
     callback(null, isMatch); 

    }); 
} 

module.exports.getUserByEmail = function(email, callback){ 
    var query = {email:email}; 
    User.findOne(query, callback); 
} 

module.exports.getUserById = function(id, callback){ 
    User.findById(id, callback); 
} 

mongodb的 - 用戶集合對象

db.users.find().pretty(); 
{ 
     "_id" : ObjectId("594c330d520d491b18771425"), 
     "name" : "John Doe", 
     "email" : "[email protected]", 
     "password" : "$2a$10$aFt9vfsSVJpFA8CMlCYIaOqQjW.6lV/1i0PibLelC43HscaAIvQPW", 
     "__v" : 0 
} 

回答

1
passport.serializeUser(function(user, done) { 
    done(null, user.id); 
}); 
passport.deserializeUser(function(user, done) { 
    User.getUserById(id, function(err, user) { 
    done(err, user); 
}); 
}); 

如果我們序列化用戶對象,然後deserilaize用戶對象不只是ID

請在此處檢查https://github.com/rupalipemare/Mongoose-Demo的例子,其中有完整的例子證明護照認證。

0
router.post('/login', passport.authenticate('local', {failureRedirect: '/login', failureFlash: 'Invalid username or password'}), function(req, res) { 

是不是'./login'?

+0

不,這是正確的。我的所有路線都沒有DOT。如果我像上面提到的那樣添加DOT,它會引發未發現的錯誤。 –