Home > Back-end >  Async method always returning true
Async method always returning true

Time:02-25

I am implementing a login functionality with firebase and React Native. The handleLogin method is always returning succeed(). If I remove the success(), I get :

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'result2.failed')]

export default async function login(credentials) {
    let result2= succeed();
    result2 = await handleLogin(credentials.email, credentials.password);
    if(result2.failed()){
       return fail({password: "Login failed"});
    }
    else{          
       return succeed();
    } 
}

const handleLogin = async(email,password) => {
    auth.signInWithEmailAndPassword(email, password)
    .catch(function(error) { 
         console.log(error); 
         return fail("failed");
     })

     return succeed(); // or fail() in which case method always returns fail()
         

I have also tried using .then() but I keep getting object undefined error if I don't return succeed() or fail() in the end of handleLogin():

auth.signInWithEmailAndPassword(email, password)
    .then(function(){return succeed();})
    .catch(function(error) { 
        console.log("we fail" error); 
        return fail("failed");
    })

CodePudding user response:

Its a known issue of the firebase. Similar thing happened to me on angular code.

CodePudding user response:

I solved the problem by adding a return statement:

return auth.signInWithEmailAndPassword(email, password).then(function(){return succeed();}).catch(function(error) { return fail("Login Attempt Failed\n" error);})
  • Related