簡體   English   中英

為什么我必須在用戶登錄后使用 firebase 對其進行身份驗證?

[英]Why do I have to authenticate the user with firebase after he has already signed-in?

我已經按照文檔在我的 android 應用程序中使用 firebase 身份驗證實現了 google 登錄。 但是,我仍在努力理解代碼及其背后的邏輯。
因此,在用戶使用他的 gmail 帳戶成功登錄后,將調用firebaseAuthWithGoogle方法,並將帳戶信息作為參數傳遞:

firebaseAuthWithGoogle(account); 

這是它的定義:

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        // We will put the data coming from the google account in MySQL database
        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mFirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = mFirebaseAuth.getCurrentUser();

                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());

                    }

                    // ...
                }
            });
}

}

我已經嘗試根據文檔向自己解釋這一點:

用戶成功登錄后,

GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account); // Calling firebaseAuthWithGoogle

GoogleSignInAccount 對象獲取ID 令牌,將交換為Firebase 憑據

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

並使用 Firebase 進行身份驗證

mFirebaseAuth.signInWithCredential(credential)

使用Firebase 憑據

對我來說不是100% 清楚的是:
1. 如果用戶已經登錄,為什么我必須通過 Firebase 進行身份驗證?
2. 如果用戶已經登錄,Firebase 身份驗證的作用是什么?
3. 如果用戶已經登錄,使用 Firebase 憑據登錄意味着什么?

我知道這對你們中的一些人來說可能聽起來微不足道,但對我來說,整個登錄流程非常模糊,尤其是 Firebase 身份驗證。

當您使用 firebase 進行身份驗證時,您可以通過執行以下操作訪問當前登錄的用戶:

FirebaseUser user = mFirebaseAuth.getCurrentUser();

通過檢索user ,您還可以檢索可用於連接到 Firebase 數據庫的userId

您還可以在再次打開應用程序時檢查用戶是否仍處於登錄狀態:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}

並在登錄后將用戶重定向到頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM