验证参数并重定向到经过验证的参数?

时间:2016-02-11 04:59:37

标签: javascript node.js express login passport-local

我试图创建一个拥有不同用户的应用。用户可以登录并查看和操作相同的数据。我想为什么当我只能创建用户对象列表并对其进行身份验证时,为每个用户创建具有相同数据的完整不同文档。我想设置看似users/user1 users/user2的路线。用户1无法转到网址栏并将user1更改为user2我在尝试制作时遇到问题,以便用户只需输入即可更改网址。

所以我有一个包含这样的用户列表的文档:

{ list: 'userList',
  users:
   [ { password: 'pass1', username: 'user1' },
     { username: 'user2', password: 'pass2' },
     { username: 'user3', password: 'pass3' } ],
  address: '14453 ny 14456',
  desc: '3 floors',
  __v: 0,
  _id: 56baf181356641f01213295a }

我的护照设置如下

passport.use("local", new LocalStrategy(
    function(username, password, done){
        User.findOne({ "users": { $elemMatch :{username : username}} }, function(err, user){
            // console.log("user " + user)
            if(err){return done(err);}
            if(!user){
                console.log("no user")
                return done(null, false,{message : "Incorrect username."});
            }
            if(!user.validPassword( username, password)){
                return done(null, false,{message : "Incorrect password."});
            }
            return done(null, user)
        })
    }))

这部分我觉得我真的很蠢。在users/:name上有一个表单,当他们提交表单时,我希望将用户重定向回users/:name。我明白该怎么做。但我试图通过添加一个计数器让用户只能查看他们的网址来修复该部分,如果该计数器小于1,则将名称设置为变量name所以name无法再分配。所以你只能比较一次这个名字。这一切都搞砸了。

基本上我需要在用户提交表单时定向到原始网址(users/:name)。用户不应该在URL栏中删除user1(:name)并键入user2。他必须自己登录

app.post("/login", function(req, res, next){
    console.log("posted")
    passport.authenticate("local",{
        successRedirect : "/users/" + req.body.username,
        failureRedirect : "/login",
        failureFlash : true
    })(req, res, next)
} )
var counter = 0
app.get("/users/:name",  ensureAuthenticated, function(req, res){
    // console.log(req.params.name)
    counter++;
    if(counter <= 1){
        var name = req.params.name
    }

    User.findOne({"users" : { $elemMatch: { username : name}}}, function(err, doc){
        if(!doc){
            res.redirect("../login")
            counter = 0;
        } else{

            console.log("test ", doc);
            var users = doc.users;
            var currentUser = {};

            for(var i = 0; i < users.length; i++){
                if(users[i].username === name){
                    currentUser = users[i];
                }
            }
            console.log("currentUser ", currentUser)
            if(currentUser.username === name ){
                res.render("users", {result : doc, user : currentUser.username});
            }           
        }


    })

})

app.post("/users", function(req, res){
    // console.log(req.protocol + "://" + req.get("host") + req.originalUrl )
    // console.log(req.body.subject, " " ,  req.protocol , " ", req.get("host") , " " ,  req.originalUrl)
    res.redirect("back")
    // res.redirect(req.protocol + "://" + req.get("host") + req.originalUrl + req.params.name)
})

玉:

html
    head
    body
        p Welcome #{user}
        p= result.address
        h3= result.desc
        a(href="/logout") logout
        form(action = "/users" method = "POST")
            label delete:
            input(type = "text" name="subject")
            input(type="submit" value = "delete")

0 个答案:

没有答案