2017-01-08 61 views
0

我想實現匿名認證,然後谷歌身份驗證數據sayc。Android應用程序的匿名身份驗證流程?

這裏是匿名身份驗證

mAuthListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
       user = firebaseAuth.getCurrentUser(); 
       if (user != null) { 
        // User is signed in 
        userNameText.setText(user.getDisplayName()); 
        Picasso.with(MainActivity.this).load(user.getPhotoUrl()).into(profileImageView); 
        Log.d(TAG, "onAuthStateChanged: " + user.getDisplayName()); 
       } else { 
        // User is signed out 
        Log.d(TAG, "onAuthStateChanged: user is null"); 
        mAuth.signInAnonymously() 
          .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() { 
           @Override 
           public void onComplete(@NonNull Task<AuthResult> task) { 
            Log.d(TAG, "signInAnonymously:onComplete:" + 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.d(TAG, "signInAnonymously", task.getException()); 
             Toast.makeText(MainActivity.this, "Authentication failed.", 
               Toast.LENGTH_SHORT).show(); 
            } 

            // ... 
           } 
          }); 
       } 
      } 
     }; 

代碼,然後用戶可以使用谷歌帳戶

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 
     mAuth.getCurrentUser().linkWithCredential(credential) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         Log.d(TAG, "linkWithCredential:onComplete:" + 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()) { 
          task.getException().printStackTrace(); 
          Toast.makeText(MainActivity.this, "Authentication failed.", 
            Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 

這運行正常選擇標誌。但即使重啓應用程序,我也沒有獲得user.getDisplayName。在Firebase控制檯中,它顯示用戶鏈接到匿名帳戶。 而且當同一用戶重新安裝應用程序時,會創建一個新的匿名用戶,但這次用戶選擇與以前相同的Google帳戶時,它不會鏈接到控制檯中已存在的用戶。請求幫助。

回答

0

你的問題是recently addressed on firebase-talk(我發現它與「firebase匿名認證應用程序重新安裝」的搜索)。

匿名身份驗證帳戶不會在應用程序卸載之間持續存在。卸載應用程序時,本地保存的所有內容都將被刪除,包括標識該帳戶的匿名身份驗證令牌。沒有簡單的方法爲最終用戶回收該令牌。相反,您應該鼓勵最終用戶完全登錄受支持的帳戶提供商,以便他們可以從所有設備登錄而不用擔心丟失數據。

+0

感謝您的鏈接,我的問題是如何重新安裝後重新安裝用戶登錄後使用相同的身份驗證提供程序將用戶連接到以前登錄的帳戶? – KiDa

+0

我很困惑。這聽起來不像你上面提到的問題,這幾乎完全是關於匿名身份驗證。你能否重寫這個問題以便反映你想知道的內容?也許給一組非常具體的步驟,導致一些沒有意義的行爲? –

相關問題