我使用护照本地策略和本地护照mongoose来创建用户并检查用户身份验证。但是,我想使用电子邮件而不是用户名来探索该选项。我按照文件的说法进行操作,并且我未经授权。有人能告诉我为什么我会收到错误,如果我只是使用用户名作为登录,它就有效。
来自护照本地猫鼬 注意:usernameField:指定包含用户名的字段名称。默认为“用户名”。如果要使用其他字段来保存用户名,例如“email”,则可以使用此选项。
从护照本地策略 默认情况下,LocalStrategy希望在名为username和password的参数中查找凭据。如果您的网站更喜欢以不同方式命名这些字段,则可以使用选项更改默认值。
我试图同时将两者都设置为true,并且我也尝试将一个设置为true,另一个设置为off。我会得到同样的错误
表达4.16.0 快递会议1.15.6 猫鼬5.1.2 护照0.40 护照本地1.0.0 护照当地猫鼬5.0.0
passport.js
module.exports = function (passport, LocalStrategy, User) {
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true,
session: true
},
function(req, username, password, done) {
return done(null, req.user);
}
));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
};
模型/ user.js的
var UserSchema = new mongoose.Schema({
name: { type: String, required: true, default: ''},
username: { type: String, unique: true, uniqueCaseInsensitive: true, required: true, default: ''},
email: { type: String, required: true, unique: true, uniqueCaseInsensitive: true, default: ''},
profileImage:{ type: String, default: ''},
timestamp: {type: String, default: () => moment().format("dddd, MMMM Do YYYY, h:mm:ss a") }
});
UserSchema.plugin(passportLocalMongoose, {usernameField: 'email'});
UserSchema.plugin(uniqueValidator);
module.exports = mongoose.model('UserSchema', UserSchema);
signinForm.js
<form class="form-signin" action="/users/signup" method="POST" enctype='multipart/form-data'>
<h1 class="h3 mb-3 font-weight-normal">Please sign up</h1>
<label for="inputName" class="sr-only">Name</label>
<input type="bane" id="inputName" class="form-control" placeholder="Name" name='name' required autofocus />
<label for="inputUsername" class="sr-only">Username</label>
<input type="bane" id="inputUsername" class="form-control" placeholder="Username" name='username' required autofocus />
<label for="inputEmail" class="sr-only">Email</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email" name='email' required autofocus />
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" name='password' required autofocus/>
<label for="inputConfirmPassword" class="sr-only">Confirm Password</label>
<input type="password" id="inputConfirmPassword" class="form-control" placeholder="Confirm Password" name='confirmPassword' required autofocus/>
<input type='file' class='form-control' id='inputFile' name='profileImage' />
<div style="height: 10px"></div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
</form>
路由/ users.js
router.post('/signup', upload.single('profileImage'), function(req, res, next) {
const name = req.body.name;
const email = req.body.email;
const password = req.body.password;
const confirmPassword = req.body.confirmPassword;
if (req.file) {
console.log('Uploading File...');
var profileImage = req.file.filename;
} else {
console.log('No File Uploaded... Setting to no image');
var profileImage = 'noimage.jpg';
}
UserController.createUser(req.body, function(err, user) {
if (err) {
res.json({
err: err
});
return;
}
passport.authenticate('local')(req, res, function() {
//res.render('index', {currentUser: user, title: 'Product Page' });
res.redirect('/');
return;
});
return;
})
});
我最终获得授权和401
提前致谢。
答案 0 :(得分:0)
尝试删除UserController.createUser()末尾的'return',你需要等待passport.authenticate()的结果