2017-08-06 117 views
1

我的使用案例是我想問新近註冊的用戶,以豐富他們的姓名等基本信息。 所以我希望能做到這一點,如:Firebase Auth,如何知道註冊新用戶,而不是現有用戶登錄?

firebase.auth().onAuthStateChanged(function(user) { 
    if (user) { 
    // User is signed in. 
    if (some indicator tells me it is newly signed up user) 
     {redirect to a form to fill in more info} 
    } else { 
    // No user is signed in. 
    } 
}); 

我查了文檔,無法找到與此相關的任何東西......

感謝提前的幫助。

+0

我將用戶存儲在數據庫中,並帶有「onboarded」字段。我檢查了這一點,以確保它們在繼續使用之前已經上報。在此身份驗證狀態中沒有「開箱即用」功能。 – theblindprophet

回答

2

由於4.6.0版本:https://firebase.google.com/support/release-notes/js#4.6.0 你可以,如果用戶是新的或現有的2種方式獲得:

  1. 如果要取回一個UserCredential結果,檢查result.additionalUserInfo.isNewUser

  2. 檢查firebase.auth().currentUser.metadata.creationTime === firebase.auth().currentUser.metadata.lastSignInTime

此前,你必須自己做,並且記住使用Firebase實時數據庫的用戶跟蹤。當用戶登錄時,您檢查數據庫中是否存在具有指定uid的用戶。如果找不到該用戶,則它是一個新用戶,然後可以將該用戶添加到數據庫。如果用戶已經在數據庫中,那麼這是一個返回的現有用戶。這裏是iOS中的一個例子。

Handing Firebase + Facebook login process

+0

謝謝。到目前爲止,這也是唯一的想法。畢竟,在Firebase身份驗證控制檯中,我希望auth本身提供API來執行此操作,它擁有註冊用戶列表。如果我可以直接在auth控制檯中查看uid列表,那將會非常棒。但只是一廂情願的想法... – LeonTan

+0

Firebase添加了一個標誌來檢查用戶是新的還是現有的:https://firebase.google.com/support/release-notes/js#4.6.0 – bojeil

+0

我在這裏找到它:任務.getResult()。getAdditionalUserInfo()。isNewUser() –

1

有一兩件事你可以做的是做事情的註冊功能的回調函數,該函數註冊做回一個承諾。你可以做這樣的事情:

firebase.auth().createUserWithEmailAndPassword(email, password) 
.then(function(user) { 
    //I believe the user variable here is the same as firebase.auth().currentUser 
    //take the user to some form you want them to fill 
}) 
.catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    // ... 
}); 

enter image description here

不過,我真的不建議做這種方式,因爲客戶端代碼是不可靠的。想一想,如果用戶在填寫表單之前突然斷開連接,該怎麼辦?他們的數據在您的數據庫中不完整。因此,如果您這樣做,請在提交表單時在用戶個人資料中設置一個標誌,以便知道誰填充了詳細信息,誰不填寫。

另一個更好的方法是使用firebase雲功能。你可以在你的雲功能中擁有這樣的代碼。雲功能是用node.js編寫的,所以你不需要花時間在另一種語言上。

exports.someoneSignedUp = functions.auth.user().onCreate(event => { 
    // you can send them a cloud function to lead them to the detail information form 
    //or you can send them an welcome email which will also lead them to where you want them to fill detailed information 
}); 

這種方式好多了,因爲您可以安全地假設您的雲服務器永遠不會關閉或受到威脅。有關雲功能的更多信息,請參閱他們的文檔:https://firebase.google.com/docs/functions/auth-events