2016-09-17 24 views
12

我在Android應用程序中使用Firebase認證,並且使用Google帳戶認證作爲登錄應用程序的選項。檢查用戶是否在Firebase中的首次被認證Google Android的認證

如何知道用戶是否第一次登錄應用程序?

任何幫助非常感謝。

+0

所以要使用檢索使用火力地堡驗證所有用戶的列表? –

+0

是的,這正是我正在尋找的 – rainman

+0

不幸的是,您不能以這種方式使用Firebase身份驗證。您必須遵循@NarenderReddy的說法 - 每當用戶第一次登錄時,您需要使用Firebase數據庫(或您的首選存儲方法)以及您想要跟蹤的信息來存儲某種條目。然後,您可以使用該數據庫位置來獲取所有用戶的列表。 –

回答

-1

當用戶首次註冊要求他重設密碼。 您可以發送密碼重置郵件用戶與sendPasswordResetEmail方法.. 鏈接:https://firebase.google.com/docs/auth/android/manage-users

然後用戶獲得重置密碼選項,並更改密碼,並與新的憑據登錄,如果用戶給出錯誤的郵件ID那麼他就不能登錄..

+0

我只是想檢查用戶是否登錄第一或不是這就是它。 我不想重置任何東西。 – rainman

+0

使用firebase實時database.when用戶登錄創建一個孩子到rootnode –

+0

我已經這樣想,但在Firebase身份驗證控制檯中,您可以看到所有用戶在您的應用程序註冊,所以我說它應該是一種方法來檢索他們的電子郵件例子... – rainman

1

解決方案時,你同時使用火力地堡驗證與公司的FireStore

該公司的FireStore數據庫的根目錄結構是這樣的 enter image description here

使用Firebase身份驗證currentUser.uid爲每個用戶創建一個根文檔。首先在第一次創建用戶時立即將名爲registered_at的字段添加到根文檔,然後根據您的用例將特定集合添加到根文檔。

登錄或登錄時,您可以檢查是否存在registered_at字段的文檔。如果它不存在,那麼你可以把用戶作爲一個新用戶(假設用戶無法刪除或更新版本改變registered_at場)

import com.google.firebase.auth.FirebaseAuth 
import com.google.firebase.firestore.DocumentReference 
import com.google.firebase.firestore.FirebaseFirestore 

fun rootDocument(): DocumentReference? = rootPath()?.let { 
    return fireStore().document(it) 
} 

fun rootPath(): String? { 
    val loggedInUser = loggedInUser() 
    if (loggedInUser != null) { 
     return "users/${loggedInUser.uid}" 
    } 
    return null 
} 

fun fireStore() = FirebaseFirestore.getInstance() 

fun createWriteBatch() = fireStore().batch() 

fun loggedInUser() = fireAuth().currentUser 

fun fireAuth(): FirebaseAuth = FirebaseAuth.getInstance() 

fun afterSignIn() { 

    val rootDocument = rootDocument() 
      ?: throw IllegalStateException("root document not found") 

    rootDocument.get().addOnCompleteListener { 
     val isNewUser = it.result.exists().not() 

     if (isNewUser) { 
      val batch = createWriteBatch() 

      batch.set(rootDocument, HashMap<Any, Any>().apply { 
       put("registered_at", System.currentTimeMillis()) 
      }) 

      batch.commit().addOnCompleteListener { 
       println("this is a new user") 
      } 

     } else { 
      println("this is not a new user") 
     } 
    } 
} 
0

雖然我完全同意,最正確的方式(給出無法將新字段添加到auth用戶表)是爲用戶創建一個新路徑並將信息存儲在那裏,我不想在登錄後進行額外的請求來執行此檢查(我正在使用Firestore並請求=錢)。

我需要做這第一次登錄檢查,以提示輸入一個用戶名(因爲顯示名稱是從Facebook /谷歌拉,我想提供重寫的選項,如果它是他們的第一次登錄)。我最終做的是使用photoURL屬性作爲標誌來確定它是否是他們的第一次。這並不理想,但也許有人想要保存請求可以使用此作爲解決方法。它並不像什麼大不了的事了火力地堡,但對於公司的FireStore它是你的計劃

2

要檢查它是否是第一次用戶登錄更昂貴,只需撥打OnCompleteListener.onComplete回撥中的AdditionalUserInfo.isNewUser()方法即可。

下面的示例代碼,請務必檢查爲空。

OnCompleteListener<AuthResult> completeListener = new OnCompleteListener<AuthResult>() { 
     @Override 
     public void onComplete(@NonNull Task<AuthResult> task) { 
      if (task.isSuccessful()) { 
       boolean isNew = task.getResult().getAdditionalUserInfo().isNewUser(); 
       Log.d("MyTAG", "onComplete: " + (isNew ? "new user" : "old user")); 
      } 
     } 
    }; 

參考了AdditionalUserInfo
https://firebase.google.com/docs/reference/android/com/google/firebase/auth/AdditionalUserInfo