2017-12-06 2 views
-1

analysierten immer ich versuche bcrypt zu verwenden, um die Hash-Werte in meinem db für Passwörter, aber die Json Antwort unten ist zur Zeit der Rückkehr gespeichert zu überprüfen:json Wert

[ 
    { 
     "password": "$2a$10$8/o1McdQ24pL5MU7dkbhmewkjne83M2duPKp0cb6uowWvOPS" 
    } 
] 

Dies bedeutet einen Fehler verursacht auf der bcrypt zu vergleichen, weil es die gesamte Antwort anstatt nur den darin enthaltenen Hash vergleicht. Wie kann ich nur den Wert des Hashes in meiner Antwort erhalten? Unten ist mein Code:

app.get('/checkHash/:username/:pass', function(req, res) { 
    console.log('below is the data'); 
    console.log(req.params); 
    var pass = req.params.pass 

     var createPromise = interact.getHash(req.params.username); 
     //did promise 
     createPromise.then(function(createResponse) { 
      //below checks to see if the password value matches the hash 
      if(bcrypt.compareSync(pass, createResponse)) { 
       //this means that the hashes are the same for user login 
       res.json("yes"); 
      } else { 
       //this means that the password hashes didn't match 
       res.json("no"); 
     } 
     }).catch(function(err) { 
     console.log(err); 
    }); 
}); 

Antwort

1

Ihre Antwort ist offenbar ein Objekt-Array. Wenn Sie nur das erste Ergebnis Ihres Arrays vergleichen möchten, müssen Sie den Index an Ihr Array übergeben.

app.get('/checkHash/:username/:pass', function(req, res) { 
    console.log('below is the data'); 
    console.log(req.params); 
    var pass = req.params.pass 

     var createPromise = interact.getHash(req.params.username); 
     //did promise 
     createPromise.then(function(createResponse) { 
      //below checks to see if the password value matches the hash 
      if(bcrypt.compareSync(pass, createResponse[0].password)) { 
       //this means that the hashes are the same for user login 
       res.json("yes"); 
      } else { 
       //this means that the password hashes didn't match 
       res.json("no"); 
     } 
     }).catch(function(err) { 
     console.log(err); 
    }); 
}); 
+0

diese noch gibt das gesamte JSON-Objekt für Passwort, ich suche nur der String-Wert des Passworts: '„$ 2a 10 $ 8 $/o1McdQ24pL5MU7dkbhmewkjne83M2duPKp0cb6uowWvOPS“' – Drew

+0

Dann brauchen Sie nur noch die Eigenschaft des Objekts zugreifen. Entweder mit 'createResponse [0] .password' oder' createResponse [0] ['password'] '. Die Antwort wurde aktualisiert – kentor