Thalita Caetano
Thalita Caetano

Reputation: 11

Authentication Firebase

I'm trying to do a simple user registration login on Firebase, but I'm receiving this error:

Uncaught ReferenceError: FirebaseAuthClient is not defined

Here is my code:

var authClient = new FirebaseAuthClient(rootRef, function(error, user) {
  if (error) {
    alert(error);
    return;
  }
  if (user) {
    // User is already logged in.
    doLogin(user);
  } else {
    // User is logged out.
    showLoginBox();
  }
});


function showLoginBox() {

  document.getElementById("#registerButton").addEventListener("click", function() {
    var email = document.getElementById("#email").val();
    var password = document.getElementById("#password").val();
    authClient.createUser(email, password, function(error,  user) {
      if (!error) {
        doLogin(user);
      } else {
        alert(error);
      }
    });
  });
}

Upvotes: 1

Views: 456

Answers (3)

Elonelon
Elonelon

Reputation: 78

i got this from firecast, you can login and register new user. It's pretty good for me, i use it with email and password auth, so no google auth. I also add simple redirect page when you already login and not login. if you alreay login, you will redirect to dashboard.html, and if you not login, you will redirect to login.html. sorry for my bad english.

"use strict";
    const txtemail      = document.getElementById('txtemail');
    const txtpass       = document.getElementById('txtpass');
    const btnlogin      = document.getElementById('btnlogin');
    const btnreg        = document.getElementById('btnreg');
    const btnout        = document.getElementById('btnout');
    const texthiden     = document.getElementById('txthide');

    btnlogin.addEventListener('click', e => {
        const email = txtemail.value;
        const pass  = txtpass.value;
        const auth  = firebase.auth();

        const promise = auth.signInWithEmailAndPassword(email, pass);
        promise.catch(e => console.log(e.message));
    });
    btnreg.addEventListener('click', e => {
        const email = txtemail.value;
        const pass  = txtpass.value;
        const auth  = firebase.auth();
        const promise = auth.createUserWithEmailAndPassword(email, pass);
        promise.catch(e => console.log(e.message));
    });
    firebase.auth().onAuthStateChanged(firebaseUser =>{
        if(firebaseUser){
            console.log(firebaseUser);
            btnout.classList.remove('hide');
            window.location = "dashboard.html";
        }else{
            console.log('not login');
            btnout.classList.add('hide');
            window.location = "login.html";
        }
    });
    btnout.addEventListener('click', e => { 
        firebase.auth().signOut();
    });
you also can hide sign out button if you want, like <button id="btnout" class="btn btn-default btn-group-lg hide">Logout</button>. and javascript will unhide your sign out button btnout.classList.remove('hide');

Upvotes: 0

GomathiSelvakumar
GomathiSelvakumar

Reputation: 494

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;         
mAuth = FirebaseAuth.getInstance();
      mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // ...
        }
    };

Firebase token get from fcm FirebaseInstanceIdService.

mAuth.signInWithCustomToken(firebase_token)
                    .addOnCompleteListener(getActivity(), new     OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            System.out.println("PRINT_DATACHANGE TASK  :" + task.isSuccessful());

                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            if (!task.isSuccessful()) {
                                Log.w(TAG, "signInWithCustomToken", task.getException());
                       /* Toast.makeText(getActivity(), "Authentication failed.",
                                Toast.LENGTH_SHORT).show();*/
                            } else {
                                Log.w(TAG, "signInWithCustomToken_else");

                            }
                        }
                    });

Upvotes: 0

Jorge Barrios
Jorge Barrios

Reputation: 507

In order to avoid problems with the FirebaseAuthClient class, you could try using the recently updated features for Authorization in Firebase:

https://firebase.google.com/docs/auth/ios/password-auth#create_a_password-based_account

After reading the new docs and creating an app, I found the easiest solution for registering users to be an e-mail & password auth. Hopefully that's what you're looking for.

Here is some sample code:

 import Firebase

 //Register user
 FIRAuth.auth()?.createUserWithEmail(email: String, password: String) { (user, error) in
        if (error != nil){
            print(error!.localizedDescription)
            return
        }
 //user registered
 }

 //Login user
 FIRAuth.auth()?.signInWithEmail(email: String, password: String) { (user, error) in
        if (error != nil){
            print(error!.localizedDescription)
            return
        }
 //user logged in
 }

 //Check if user is signed in
 if let user = FIRAuth.auth()?.currentUser {
 // User is signed in.
 } else {
 // No user is signed in.
 }

Now, go get those users!

Upvotes: 1

Related Questions