Search code examples
javascriptstringmeanjs

Two identical strings not shown as equal Javascript Meanjs


This is an extremely simple and basic problem, to the extent that I am baffled as to what could be causing this behavior. In my MEANJS app, the user is being logged in using Passport. The User model has a simple authenticate method, which matches the entered password, with the user's password, after hashing it. This app is already in production, and there is nothing wrong with it. I have been facing an altogether different problem of not being able to update users, because Save fails as the password field is required, which when given, results in rehashing of the password. Point being, I haven't messed with the authenticate method in the least, it's the standard MEANJS authenticate function.

UserSchema.methods.authenticate = function(password) {
 console.log('password being authenticated is ' + password);
 console.log('Hash password is : ' + this.hashPassword(password));
 console.log('User\'s existing pwd is : ' + this.password);

 var ePwd = this.hashPassword(password);
 var uPwd = this.password;
 if(ePwd === uPwd){
    console.log('TRUE'); //THIS IS NOT DISPLAYED
 }
 return this.password === this.hashPassword(password);
};

This is not returning true, even though my passwords are exactly the same. To check and since I am logging the values I copy pasted them in my Chrome ScratchJS window just to verify and this displays correctly there:

var p1 = 'nkzm3dT0tjxOZ'; //hashPassword(password)
var p2 = 'nkzm3dT0tjxOZ';  //this.password
if(p1 === p2){console.log('TRUE');} //WORKS

Why on earth would this happen?? There can be no possible interference from any changes I may have made as the values are being retried right here. I am sure it is possibly something infinitely stupid, but this is really driving me up the wall. I would really appreciate any help, brickbats included.

Console output

password being authenticated is mypassword
Hash password is : nkzm3dT0tjxOZSRfsoNckNomgVouYvaw7WYEpqL4G6ovFTSDeosnxfHecNUhsvDWwcXIBzopsvLx1HZiX+

User's existing pwd is : nkzm3dT0tjxOZSRfsoNckNomgVouYvaw7WYEpqL4G6ovFTSDeosnxfHecNUhsvDWwcXIBzopsvLx1HZiX+

I must point out that this has been working PERFECTLY fine up until just a few hours ago.

TYPEOF result

typeof ePwd string
typeof uPwd string

Solution

  • Trim leading/trailing whitespaces from epwd and upwd.