身份验证Firebase

时间:2016-05-26 21:52:45

标签: firebase firebase-authentication

我正在尝试在Firebase上进行简单的用户注册登录,但我收到此错误:

  

未捕获的ReferenceError:未定义FirebaseAuthClient

这是我的代码:

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);
      }
    });
  });
}

3 个答案:

答案 0 :(得分:1)

为了避免FirebaseAuthClient类出现问题,您可以尝试在Firebase中使用最近更新的功能进行授权:

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

在阅读新文档并创建应用程序后,我找到了将用户注册为电子邮件的最简单的解决方案。密码验证。希望这是你正在寻找的东西。

以下是一些示例代码:

 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.
 }

现在,去找那些用户!

答案 1 :(得分:0)

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令牌来自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");

                            }
                        }
                    });

答案 2 :(得分:0)

我从firecast获得此信息,您可以登录并注册新用户。这对我来说非常好,我使用电子邮件和密码验证,所以没有谷歌认证。我还在您登录时添加了简单的重定向页面,而不是登录。如果您登录,则会重定向到dashboard.html,如果您未登录,则会重定向到login.html。对不起,我的英语不好。

&#13;
&#13;
"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();
    });
&#13;
&#13;
&#13; 你也可以隐藏退出按钮,如<button id="btnout" class="btn btn-default btn-group-lg hide">Logout</button>。并且javascript会取消隐藏您的退出按钮btnout.classList.remove('hide');