【发布时间】: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是来自 HTMLform的值吗?如果是这样,它将在req.body值中访问,而不是req.query。req.query公开 URL 查询参数的值。 -
实际上,您似乎引用了
res.query.username。这无效 - 你的意思是req.query.username?
标签: javascript node.js express passport.js