无法使用护照登录 - Nodejs
Posted
技术标签:
【中文标题】无法使用护照登录 - Nodejs【英文标题】:Unable to login using passport - Nodejs 【发布时间】:2017-11-26 17:02:02 【问题描述】:我正在关注 Udemy 教程之一,但我陷入了登录过程的最后一点。我可以注册新用户但无法登录。我输入电子邮件地址和密码。在按下提交按钮时,无论电子邮件地址或密码的值是正确还是错误,它都会触发护照的failureRedirect
方法再次拉动登录页面,我已经放置了几个console.logs,但没有一个打印在控制台中。此外,如果输入任何随机细节,它应该会引发未知用户错误。我不确定是什么导致它无法工作,在控制台或浏览器中没有看到或发现错误。
表单 - 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" : "john@gmail.com",
"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);
);
);
如果我们序列化用户对象,那么反序列化用户对象而不仅仅是 id
请查看https://github.com/rupalipemare/Mongoose-Demo上的示例,其中有完整示例演示护照身份验证。
【讨论】:
【参考方案2】:router.post('/login', passport.authenticate('local', failureRedirect: '/login', failureFlash: 'Invalid username or password'), function(req, res)
不是'./login'吗?
【讨论】:
不,这是正确的。我所有的路线都没有 DOT。如果我像你提到的那样添加 DOT,它会引发未找到错误。以上是关于无法使用护照登录 - Nodejs的主要内容,如果未能解决你的问题,请参考以下文章
尝试通过护照linkedin集成linkedin登录时“无法在会话中找到请求令牌”
无法运行护照登录策略。身份验证错误~ TypeError: Cannot read property 'authenticate' of undefined