【问题标题】:getting form data from POST using express.js with passport.js使用 express.js 和 passport.js 从 POST 获取表单数据
【发布时间】:2018-03-31 04:56:20
【问题描述】:

所以我建立了一个聊天室,一切正常,直到我将系统更改为仅使用 uid,以下是它的工作原理的一些简要说明

当用户注册时,他会被分配一个 uid,该 uid 连同他的凭据一起放入数据库中,因此每次用户登录时,它都会要求服务器获取与您在文本框中输入的用户名相关联的 uid 和把它设置成这样的cookie

setCookies() 在提交时被调用

function setCookies() {
    var username = document.getElementById("login-username").value
    console.log(username)
    getLoggedInUserInfo(username)
}
function getLoggedInUserInfo(username){     
console.log(username)
socket.emit('getLoggedInUserInfo',username)
}
socket.on('loggedInUserInfo',function(uid){
    document.cookie = "uid=" + uid;
})

但这是注册页面上的一个问题,因为用户在数据库中还没有关于他的任何信息

所以我在想如果我可以使用 express 在服务器端设置 cookie 但我无法使用这样的 express 从表单中获取任何值

app.post('/apolloLogin', passport.authenticate('local-login', {
    successRedirect: '/apolloMainchat', // redirect to the secure profile section
    failureRedirect: '/apolloLogin', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
},function(req, res){
   var username = res.query.username

    console.log("tettestset")
    //res.cookie('cookieName','randomUid', { maxAge: 900000, httpOnly: true });
}),

在这里我会得到与用户名关联的 uid 然后像这样设置uid cookie

res.cookie('cookieName','randomUid', { maxAge: 900000, httpOnly: true });

但我的用户名值总是为空,它说 res.cookie 不是函数

我在 express 上做错了什么

编辑

现在我正在尝试这个

app.post('/apolloSignup', passport.authenticate('local-signup', {
    successRedirect: '/apolloMainchat', // redirect to the secure profile section
    failureRedirect: '/apolloSignup', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
},function(req, res){
    console.log(req.body.username)
}));

第二次编辑

这就是我现在使用的,我从 console.log 得到空值

app.post('/apolloSignup', passport.authenticate('local-signup', {successRedirect: '/apolloMainchat',failureRedirect: '/apolloSignup',failureFlash: true}),function(req, res){
    console.log(req)
});

这就是我的 HTML 的样子

<html>
    <head>
            <link type="text/css" rel="stylesheet" href="css/regular.css" />
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <script src="/socket.io/socket.io.js"></script>
            <script src="scripts/jquery-1.11.1.js"></script>  
            <script src="scripts/bic_core.js"></script> 
    </head>
    <body>
        <div class="login-register-div" id="login-register-div">
                            <!-- show any messages that come back with authentication -->
                            <% if (message.length > 0) { %>
                                <div class="login-error">
                                    <script>
                                        $("#login-username").addClass("textboxError");
                                        $("#login-password").addClass("textboxError");
                                        </script>
                                    <%= message %>
                                </div>
                                <% } %>
                                    <!-- LOGIN FORM -->
            <div id="login-container" class="login-container">
                <form action="/apolloSignup" method="post">
                    <center><h1>Sign Up</h1></center>
                    <input type="text" placeholder="Username" class="login-username" id="login-username" name="username">
                    <input type="password" placeholder="Password" class="login-password" id="login-password" name="password">
                    <input type="submit" value="Signup" class="login-submit" id="login-submit" onclick="setCookies()">
                    <a class="signup-login-link" href="/apolloLogin">Log In</a>
                </form>
            </div>
        </div>
    </body>
</html>
<script>

【问题讨论】:

  • username 是来自 HTML form 的值吗?如果是这样,它将在req.body 值中访问,而不是req.queryreq.query 公开 URL 查询参数的值。
  • 实际上,您似乎引用了res.query.username。这无效 - 你的意思是req.query.username

标签: javascript node.js express passport.js


【解决方案1】:

我很确定,一旦您到达 failureRedirect / successRedirect,请求/响应周期就结束了,因此您不会遇到下一个中​​间件。但是,如果您正确设置了护照本地身份验证,您应该可以访问req.user,其中将包含登录用户的用户名和其他信息。

尝试在/apolloMainchat 路由中设置cookie,引用req.user。希望对您有所帮助!

【讨论】:

    【解决方案2】:

    您似乎引用了res.query.username。这是无效的 - 你的意思是req.query.username?查询参数将在请求对象上,而不是在响应对象上。

    此外,如果您从 HTML 表单获取 POST 数据,则应通过 req.body 而不是 req.query 访问这些值。

    【讨论】:

    • 我尝试过 console.log(req.body.username) 没有成功
    • 您能否编辑您的答案以包含您的 HTML 表单的外观?
    猜你喜欢
    • 2021-06-21
    • 2013-07-13
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 2014-12-08
    相关资源
    最近更新 更多