Validating whether or not there is a user with Firebase

Asked

Viewed 204 times

0

I’ve created a registry, web, on Firebase. I can sign up, but if I get a valid email and password, I don’t get redirected into the system. And if I put an email and any password, it does not show the phrase "Invalid user or password" that I programmed to be displayed

HTML:

<div class="container-fluid">
<div class="row">
    <div class="col-md-4"></div>
    <div class="col-md-4">
        <form class="form-signin formularioLogin">

            <h3 align="center">Domanda</h3>

            <label>E-mail do Estabelecimento</label>
            <div>
                <input class="form-control" type="email" name="email" ng-model="admin.email"
                placeholder="[email protected]">
            </div>

            <label>Senha</label>
            <div>
                <input class="form-control adminSenha" type="password" ng-model="admin.senha">
            </div>

            <button class="btn btn-block btn-success btnLogar" ng-click="logar(admin)">Entrar</button>
            <div>{{ loginErro }}</div>

            <button class="btn btn-block btn-primary btnLogar" ng-click="cadastrar(admin)">Cadastrar</button>

        </form>
    </div>
    <div class="col-md-4"></div>
</div>

Controller:

$scope.logar = function(admin){

    var email = admin.email;
    var password = admin.senha;

    firebase.auth().signInWithEmailAndPassword(email, password)
    .then( function ( result ) {
        //Usuário autenticado com sucesso
        //Seu código para redirecionar vem aqui.
        console.log(result);
        $location.path('/inicial');
    }, function ( error ) {
        //Ops, ocorreu um erro, pode deixar seu código tratando o erro aqui
        console.log('erro: '+error);
        $location.path('/');
    })


}

1 answer

0

You are only treating the error scenario, with the catch...

Since it’s a prop you should also treat what to do if it works out in the "then".

Ex:

firebase.auth().signInWithEmailAndPassword(email, password)
.then( function ( result ) {
    //Usuário autenticado com sucesso
    //Seu código para redirecionar vem aqui.
}, function ( error ) {
    //Ops, ocorreu um erro, pode deixar seu código tratando o erro aqui
})
  • See the modification I made in the code... Neither this console.log('all right!') does not appear when I type valid email and password.

  • Gustavo, you are calling the function signInWithEmailAndPassword and treating the "catch", whenever you fall in the catch, is why there has been some error, when everything goes right will never fall there, so I posted the answer using the then, the function signInWithEmailAndPassword is a Promise, where her return is a Success that you deal with the "then" or an error that you deal with with the "catch", in your case you are only treating the error

  • Replace your call with mine above, and place a console log inside the first block where you have the result which is where everything is right and then add a console.log to the error and perform some tests

  • https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword check that the return of the function is a Promise

  • Here is the documentation for using the files with Angularjs https://docs.angularjs.org/api/ng/service/$q

  • Right Bruno. But now, even though I put a valid login I am not redirected into the app.

  • I tested again... Only after a second click to log in I enter the system... You know how I can fix this?

  • The Promises are asynchronous, do a test calmly, click and wait a little, if it works, I advise to add a loading, to take away this feeling that nothing happened, it may take a little longer even.

  • Nothing... I waited half a minute and nothing kkkkk

  • Don’t fall for success or error? See if there is no error in your console, because it should work

  • No kkkkk I work monitoring the kkkk console

  • I can’t think of any scenario, if the Promise call was made, it should return a success or an error, I never saw any scenario where there is no return and only the second click would work.. :/

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.