我正在尝试保护没有角色的用户访问的路由。 然后,我继续前进,找到了本地护照角色模块并将其粘贴到我的代码中。我有一个具有用户,角色和密码的MongoDB用户集合。在拒绝投票之前,请检查我的个人资料。
let passport = require('passport')
let LocalStrategy = require('passport-local').Strategy;
app.use(bodyParser.urlencoded({extended: true}))
passport.use(new LocalStrategy(
function(username, password, role, done) {
console.log('It doesnt reach this part of the code');
User.findOne({ username: 'Baud' }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword('abr3alas')) { return done(null, false); }
return done(null, user);
});
}
));
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'passwd',
roleField: 'administrator',
passReqToCallback: false,
session: false
},
function(req, username, password, role, done) {
// request object is now first argument
console.log('It doesnt print any of the following information')
console.log('role is:' + role)
console.log('req is:' + req)
}
));
// create users collection
app.post('/users', passport.authenticate('local'),
function(req, res) {
console.log('even though I access this route, doesnt do nothing bellow');
console.log(res)
db.collection('users').find().toArray((err, result) => {
if (err) return console.log(err)
console.log(users);
});
if (req.user.role == "administrator") {
return res.send('you can see this content');
}
else {
console.log('you are not administrator')
res.send('you can not see this content');
}
(req, res) => {
names = db.collection('users').find({name: req.body.name});
console.log(names);
db.collection('users').insertOne(req.body,
(err, result) => {
if (err) return console.log(err)
console.log('User created with role ' + req.body.role);
res.redirect('/')
})
}
})
不是一个console.log可以打印任何内容。
我有一个MongoDB用户集合和我从/ users提交的表格 而且我有一个有效的MongoDB查询,可以打印出用户凭据。
users.ejs:
<form action="/users" method="POST">
<input type="text" placeholder="username" name="username">
<input type="text" placeholder="role" name="role">
<input type="password" placeholder="password" name="password">
<button type="submit" id="user" >Login</button>
</form>
“错误请求”
如果没有一个console.log应该打印什么,这应该如何工作?谢谢
答案 0 :(得分:0)
因为没有人帮助,我不得不退出