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的文件夾中定義。還要感謝附註,我一定會執行它。 –