0

I'm building a web application and need to built a login site. How do I loop through this javascript object and compare the user input on the login form (only email ("EML") needed at the moment) to the data to make sure the input is correct? I tried to make something, but only came up with the for loop below that is just a mess. (The object is in a seperate js file) Thank you!

function validate() {
    var un = document.login.username.value;
    var pw = document.login.password.value;
    var valid = false;

    for (var key in responseData) {
        if (responseData.hasOwnProperty(key)) {
            for (var i = 0; i < key.length; i++) {
                if (un == key[i]) {
                    valid = true;
                    break;
                }
            }
          }


    if (valid) {
        alert("Login was successful. Welcome, " + un + ".")
        window.location = "https://www.google.com";
        return false;
    }
}
var responseData = {
  authenticatUser: {  
   "ERR":0,
   "RSP":{  
      "AUTHC":"true",
      "USR":{  
         "ID":"2",
         "TJT":"FULL",
         "ACTV":"true",
         "BO":"1489760664786",
         "CONT":{  
            "FNM":"John",
            "LNM":"Doe",
            "PHN":"5556667777",
            "PHNTP":"NONE",
            "EML":"ex@mple.com",
            "EMLTP":"NONE"
         },
         "ADMIN":"false",
         "LLOGN":"1489760664786",
         "ACCT":{  
            "ID":"2",
            "TJT":"ID"
         }
      }
   }
},
    getUserAccountDetails: {  
   "ERR":0,
   "RSP":{  
      "ACCT":{  
         "ID":"2",
         "TJT":"FULL",
         "ACTV":"true",
         "BO":"1489760664786",
         "LU":"1489760664786",
         "NM":"Name",
         "DESC":"Description",
         "CONT":{  
            "FNM":"John",
            "LNM":"Doe",
            "PHN":"5556667777",
            "PHNTP":"NONE",
            "EML":"ex@mple.com",
            "EMLTP":"NONE"
         },
         "ADDRM":{  
            "STRT":"1 Miracle Way",
            "CITY":"San Antonio",
            "STATE":"Texas",
            "ZIP":"78245"
         },
         "ADDRB":{  
            "STRT":"1 Miracle Way",
            "CITY":"San Antonio",
            "STATE":"Texas",
            "ZIP":"78245"
         },
         "TZ":"US_CT",
         "LICS":"1",
         "REPOS":[  
            {  
               "ID":"2",
               "TJT":"ID"
            },
            {  
            }
         ],
         "USRS":[  
            {  
               "ID":"2",
               "TJT":"ID"
            },
            {  
            }
         ]
      }
   }
}

};

vanDeurs
  • 35
  • 5
  • 1
    If you're asking how to access nested properties inside a js object, you'll find the answer [right here](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). Also gotta warn you that storing and accessing user information as plain JSON isn't a very secure way to create a login page though. – Khauri Jul 15 '17 at 21:56

1 Answers1

0

First off, you shouldn't be authenticating login details client side or keep client details in plain text, I assume you know this but you're just learning.

for (var key in responseData) {
        // THIS IS REDUNDANT
        // YOU GOT THE KEY FROM THE OBJECT SO THIS ISN'T
        // NECESSARY 
        if (responseData.hasOwnProperty(key)) {
            // ITERATING OVER THE LENGTH OF THE KEY
            // DOESN'T MAKE A LOT OF SENSE
            // You're not in fact iterating over the object
            for (var i = 0; i < key.length; i++) {
                if (un == key[i]) {
                    valid = true;
                    break;
                }
            }
          }

What you want to do is simplify the json response data to something along the line of

{
   "accounts" : [
      {
       "id" : "xxxxxxx",
       "username" : "xxxxxx",
       "password" : "xxxxxx"
      },
      {
       "id" : "xxxxxxx",
       "username" : "xxxxxx",
       "password" : "xxxxxx"
      },
      {
        "id" : "xxxxxxx",
       "username" : "xxxxxx",
       "password" : "xxxxxx"
      }
  ]
}

Then you'll check for the user credentials like so:

let data = JSON.parse(responseData);
found = false;
for(var account in data.accounts)
{
    if(account.username === un && account.password === pw)
    {
        found = true;
        break;
    }
}

if(found)
{
    //POST user id back to server to get details
}

Mind you I do not advise authenticating in this way any at all. If you want to simulate well you should do a run some type of simple apache php or node server that will accept the username and password and validate server side and return to you the results.

InfinityCounter
  • 374
  • 2
  • 4
  • 14