2016-05-26 260 views
1

我試圖做一個火力地堡簡單的用戶註冊登錄,但我收到此錯誤:認證火力地堡

Uncaught ReferenceError: FirebaseAuthClient is not defined

這裏是我的代碼:

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

您從哪裏得到FirebaseAuthClient?我認爲這個班已經被棄用了2年以上。請參閱http://stackoverflow.com/questions/17487968/firebase-firebaseauthclient-class-being-deprecated –

回答

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

現在,去把這些用戶!

+0

如果您發現我的回答有用,我建議您接受它。 @Thalita Caetano –

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

          } 
         } 
        }); 
0

我從firecast得到了這個,你可以登錄並註冊新用戶。這對我來說非常好,我使用它與電子郵件和密碼驗證,所以沒有谷歌驗證。當您已經登錄並且未登錄時,我還添加了簡單的重定向頁面。如果你已經登錄,你會重定向到dashboard.html,如果你沒有登錄,你會重定向到login.html。對不起,我的英語不好。

"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(); 
 
    });
您還可以隱藏,如果你想退出按鈕,像 <button id="btnout" class="btn btn-default btn-group-lg hide">Logout</button>。 javascript會取消隱藏您的退出按鈕 btnout.classList.remove('hide');