0
我已经看到这个问题已经问了,对不起,如果它似乎重复;但我已经阅读了很多答案,他们没有帮助。无论我如何使用登录表单,我都会收到“MISSING CREDENTIALS”错误。我正尝试使用电子邮件和密码实现用户登录。用户名和密码js和mongoDB
我的路由文件
const express = require('express');
const router = express.Router();
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const User = require('../models/user');
passport.use(new LocalStrategy((email, password, done) => {
User.getUserByEmail(email, (err, user) => {
if(err) throw err;
if(!user) {
return done(null, false, {message: 'this account does not exist'});
}
User.comparePassword(password, user.password, (err, isMatch) => {
if(err) throw err;
if(isMatch) {
return done(null, user);
}
else {
return done(null, false, {message: 'oops! wrong password! try again'});
}
});
});
}));
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.getUserById(id, (err, user) => {
done(err, user);
});
});
router.post('/', (req, res, next) => {
passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/toSignInPage',
failureFlash: true
})(req, res, next);
});
module.exports = router;
user.js的文件在我的app.js文件
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcryptjs');
const UserSchema = new Schema({
email: {
type: String,
required: true,
trim: true
},
name: {
type: String,
required: true,
unique: true,
trim: true
},
password: {
type: String,
required: true,
trim: true
},
profilePhoto: {
originalemail: String,
imagePath: String
},
token: String
});
const User = module.exports = mongoose.model('User', UserSchema);
module.exports.registerUser = function(newUser, callback) {
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if(err) {
console.log(err);
}
newUser.password = hash;
newUser.save(callback);
});
});
};
module.exports.getUserByEmail = function(email, callback) {
const query = {
email: email
};
User.findOne(query, callback);
};
module.exports.getUserById = function(id, callback) {
User.findById(id, callback);
};
module.exports.comparePassword = function(candidatePassword, hash, callback) {
bcrypt.compare(candidatePassword, hash, (err, isMatch) => {
if(err) throw err;
callback(null, isMatch);
});
};
我初始化护照 - 会话选项和所有的不错的东西。
还我的HTML(把手)的形式
<form method="post" action="/signInPage">
<label for="mail"> Email </label>
<input type="email" id="mail" name="email" />
<label for="password"> Password</label>
<input type="password" id="password" name="password"/>
<button type="submit">Sign in</button>
<p class="corp"> <a href="forgot.html">Forgot password?</a> </p>
</form>
任何帮助表示赞赏这一点。我一直坚持一段时间调试,我似乎无法弄清楚我必须做错什么。如果我没有以可理解的格式正确地格式化问题,请让我知道。
我没有看到服务器上的表单POST操作。我错过了吗?作为一个方面说明,如果用户没有找到,但用户被发现但密码不正确,使得黑客更容易,那么您真的不应该以不同的方式回应用户。 – Paul
非常感谢您的回复。帖子操作实际上是“/注册”路径,在名为signup.js的文件夹中定义。还要感谢附注,我一定会执行它。 –