2017-03-13 42 views
4

我正致力於使用GitHub身份驗證提供程序將匿名用戶轉換爲永久用戶。爲什麼firebase的linkWithPopup()沒有在firebase中設置用戶`photoUrl`?

這個想法是,在我的應用程序中,用戶總是自動作爲匿名用戶進行身份驗證。然後,當他們點擊「使用GitHub登錄」按鈕時,基本上會發生什麼是我的應用首先嚐試將該匿名用戶鏈接到GitHub憑證(基本上是同時登錄),但是如果鏈接失敗,用戶已經存在(例如,之前已經鏈接過),它會回落到使用GitHub的正常登錄狀態。

我使用firebase的API linkWithPopup(provider)signInWithPopup(provider)。這兩種方法都會返回一個承諾,通過認證的GitHub用戶解決,這非常棒!但是,事實證明,我從linkWithPopup()獲得的用戶對象沒有photoUrl屬性集。它總是null

當我signInWithPopup()直線距離,甚至沒有嘗試聯繫起來,我也獲得與用戶對象解決的承諾,這一個確實photoUrl集。所以看起來像linkWithPopUp()中有一個錯誤。

這是已知問題,還是這種預期的行爲?

我的代碼如下所示:

linkOrSignInWithGitHub(): Observable<User> { 
    let linkPromise = firebase.auth() 
           .currentUser 
           .linkWithPopup(new firebase.auth.GithubAuthProvider()) 
           .then(result => result.user); 

    // If a user has been permanentely linked/authenticated already and tries 
    // to link again, firebase will throw an error. That's when we know that 
    // user credentials do already exist and we can simply sign in using GitHub. 
    return Observable.fromPromise(<Promise<any>>linkPromise).catch(error => this.signInWithGitHub()); 
    } 

而且signInWithGitHub()看起來是這樣的:

signInWithGitHub(): Observable<User> { 
    let loginPromise = firebase.auth() 
           .signInWithPopup(new firebase.auth.GithubAuthProvider()) 
           .then(result => result.user); 

    return Observable.fromPromise(<Promise<any>>loginPromise); 
} 

如此反覆,在本身的代碼工作完全正常,這只是我不明白的當使用linkWithPopup()時,用戶photoURL

回答

3

linkWithPopup不會修改現有的頂級屬性(currentUser.email,currentUser.displayName,currentUser.photoURL等)。它只會在currentUser.providerData中添加一個新的providerData對象以及與之鏈接的相應providerData。 頂級數據在註冊時設置,因爲您已經注意到了。

您可以在linkWithPopup解析後手動更新使用該提供者數據的頂級數據。 currentUser.updateProfile({ photoURL: currentUser.providerData[indexOfGitHub].photoURL }

相關問題