I am trying to make and store usernames and passwords in cleartext. I am not doing any type of authentication (I know I could be using node passport to do this, and encrypting, but I am just learning javascript, so I am just trying to play around)
我试图以明文形式制作和存储用户名和密码。我没有做任何类型的身份验证(我知道我可以使用节点通行证来执行此操作,并加密,但我只是在学习javascript,所以我只是想尝试一下)
I have an object that I have globally defined like this:
我有一个我全局定义的对象,如下所示:
var obj= {username: req.body.username,
password: req.body.password}
that I am pushing onto my registeredUsers array:
我正在推动我的registeredUsers数组:
var registeredUsers = new Array();
My issue is that I want to be able to do something like:
我的问题是我希望能够做到这样的事情:
if((($.inArray(username, registerdUsers) == username &&
($.inArray(password, registerdUsers)) == password){
res.redirect("/?error=Already Registered");
}
This doesn't work, how can I check both values of my object to see if they are contained in my array?
这不起作用,我如何检查我的对象的两个值,看看它们是否包含在我的数组中?
Here are the functions that I am doing the authentication in case anyone is curious:
以下是我正在进行身份验证的功能,以防万一有人好奇:
function ensureAuthentication(req, res, next){
//push object onto the registeredUsers array
registeredUsers.push(obj);
//if the user is already registered, throw error
if (($.inArray(username, registeredUsers) && ($.inArray(password, registeredUsers)) {//obj.contains() username){
res.redirect("/?error=Already Registered");
}
//if new user
else{
authentication.push(obj);
console.log("added new user);
//redirect to homepage
res.rediret("/");
}
}
and
function login(req, res) {
//var username = req.body.username;
req.session.username = username;
req.session.password = password;
loggedInUsers[username] = LoggedIn;
if((($.inArray(username, registerdUsers) == username && ($.inArray(password, registerdUsers)) == password){
//increase login count
for(users in loggedInUsers){
++loginCount;
console.log("Login Count: ", loginCount);
}
//redirect to login page
res.redirect("/users")
}
else{
//print out error message
res.redirect("/?error=Error: incorrect username/password");
}
}
2 个解决方案
#1
1
Find the object by username:
按用户名查找对象:
var user;
for(var i = 0; user = registeredUsers[i]; i++) {
if(user.username === username)
break;
}
Check the password:
检查密码:
var valid = user && user.password === password;
#2
1
$.inArray is like Java's indexOf function which returns the index of the position if the obj is in the array, otherwise -1
$ .inArray就像Java的indexOf函数,如果obj在数组中,则返回位置的索引,否则返回-1
So something like..
所以像..
if((($.inArray(username, registerdUsers) !== -1 &&
($.inArray(password, registerdUsers)) !== -1){
res.redirect("/?error=Already Registered");
}
..would check to make sure you don't have any duplicate users with the exact same password, but allow duplicate users.
..将检查以确保您没有任何具有完全相同密码的重复用户,但允许重复用户。
I think you are looking for something like..
我想你正在寻找像...
if($.inArray(username, registeredUsers) !== -1){
res.redirect("/?error=Already Registered");
}
which says, if the username exists in the registeredUsers array, then give the error msg 'Already Registered'
如果用户名存在于registeredUsers数组中,则输入错误消息msg'已经注册'
#1
1
Find the object by username:
按用户名查找对象:
var user;
for(var i = 0; user = registeredUsers[i]; i++) {
if(user.username === username)
break;
}
Check the password:
检查密码:
var valid = user && user.password === password;
#2
1
$.inArray is like Java's indexOf function which returns the index of the position if the obj is in the array, otherwise -1
$ .inArray就像Java的indexOf函数,如果obj在数组中,则返回位置的索引,否则返回-1
So something like..
所以像..
if((($.inArray(username, registerdUsers) !== -1 &&
($.inArray(password, registerdUsers)) !== -1){
res.redirect("/?error=Already Registered");
}
..would check to make sure you don't have any duplicate users with the exact same password, but allow duplicate users.
..将检查以确保您没有任何具有完全相同密码的重复用户,但允许重复用户。
I think you are looking for something like..
我想你正在寻找像...
if($.inArray(username, registeredUsers) !== -1){
res.redirect("/?error=Already Registered");
}
which says, if the username exists in the registeredUsers array, then give the error msg 'Already Registered'
如果用户名存在于registeredUsers数组中,则输入错误消息msg'已经注册'