1

I am using Google Cloud function to validate my OTP Authentication, and also using Firebase database to save code in the database.

My problem is, even when the If statements condition are satisfied, it always executes else statement. I am comparing code and codeValid from firebase database with the user input. Thus, my user input is satisfied with code and codevalid is also satisfied, but it always moves to else statement. I dont know why.

Here is my code

const admin = require('firebase-admin');

module.exports = function(req, res) {
if(!req.body.phone || !req.body.code) {
    return res.status(422).send({error: 'Phone and Code Must be 
Provided'});
 }

const phone = String(req.body.phone).replace(/[^\d]/g, '');
const code = parseInt(req.body.code);

return admin.auth().getUser(phone) 
    .then(() => {
        const ref = admin.database().ref('users/'+ phone);
       return ref.on('value', snapshot => {
            ref.off();
            const user = snapshot.val();

            if (user.code === code && user.codeValid === true) {
                ref.update({ codeValid: false });
                admin.auth().createCustomToken(phone)
                    .then(token => res.send({ token: token }))
                    .catch((err)=> res.status(422).send({ error:err }));
            }
            else {
                return res.status(422).send({ error: 'Code Not Valid' });
            }
        });
    })
    .catch((err)=> res.status(422).send({ error:err }) )
 } 

So, I always get "code not valid" what ever the input i give. I cross checked all the values with firebase database also, everything matches. But couldn't find why its happening.

James Z
  • 12,209
  • 10
  • 24
  • 44
Arvindh
  • 600
  • 1
  • 10
  • 25
  • Is `user.code === code && user.codeValid === true` always `true`? – sandrooco Jan 05 '19 at 16:22
  • yes, it should be always true so, that user inputs code and database stored code will be equal. And i have set codeValid as true (initially) , when this functions runs it will update the codeValid as false – Arvindh Jan 05 '19 at 16:24
  • 1
    Try with parseInt(user.code) === code && user.codeValid === true – IMParasharG Jan 05 '19 at 16:26
  • 1
    Probably it‘s not always true when the else block is run. Log it and check if the types match. – sandrooco Jan 05 '19 at 16:39
  • In order to find what's happening, I'd try to write a long message error (temporary or not; I haven't read all the code) with `user.code`, `code` and `user.codeValid`. I hope it helps you. – Rachel M. Carmena Jan 05 '19 at 16:33

1 Answers1

2

Add this above your if condition and check whether your statements are really true. I think it's possible that your datatypes are different for example for user.code and code. So you should also test it with == or with parsing your values.

// values and datatypes are equal
if (user.code === code) { 
  console.log('user.code === code');         
}

// values and datatypes are equal
if (user.codeValid === true) {
  console.log('user.codeValid === codeValid');
}

// values are equal
if (user.code == code) {
  console.log('user.code == code');         
}

// values are equal
if (user.codeValid == true) {
  console.log('user.codeValid == codeValid');
}

For more information about the difference of == and === look at this answer:

Difference between == and === in JavaScript

Sean Stayns
  • 4,082
  • 5
  • 25
  • 35
  • 1
    Thanks for the words, actually the user.code and code was in different type ! Thats the problem. Now cleared – Arvindh Jan 06 '19 at 15:51