Pas*_*cht 5 firebase firebase-authentication
I'm working on implementing converting anonymous users to permanent users using GitHub auth provider.
The idea is that in my app, users are always automatically authenticated as anonymous users. Then, when they click the "Sign in with GitHub" button, basically what happens is that my app first tries to link that anonymous user to the GitHub credentials (which is basically a signin at the same time), but if the linking fails because the user already exists (e.g. has been linked before), it falls back to a normal sign in using GitHub.
I use firebase' APIs linkWithPopup(provider) and signInWithPopup(provider). Both methods return a promise that resolves with the authenticated GitHub user which is great! However, it turns out that the user object that I get from linkWithPopup() does not have the photoUrl property set. It's always null.
When I signInWithPopup() straight away, without even trying to link, I also get a promise resolved with the user object and this one does have the photoUrl set. So it looks like that there's a bug in linkWithPopUp().
Is this a known issue, or is this expected behaviour?
My code looks like this:
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());
}
Run Code Online (Sandbox Code Playgroud)
And signInWithGitHub() looks like this:
signInWithGitHub(): Observable<User> {
let loginPromise = firebase.auth()
.signInWithPopup(new firebase.auth.GithubAuthProvider())
.then(result => result.user);
return Observable.fromPromise(<Promise<any>>loginPromise);
}
Run Code Online (Sandbox Code Playgroud)
So again, the code in itself works perfectly fine, it's just that I don't get the users photoURL when using linkWithPopup().
linkWithPopup 不会修改现有的顶级属性(currentUser.email、currentUser.displayName、currentUser.photoURL 等)。它只会在 currentUser.providerData 中添加一个新的 providerData 对象,其中包含您链接的相应 providerData 。正如您已经注意到的那样,顶级数据是在注册时设置的。
您可以在 linkWithPopup 解析后使用该提供者的数据手动更新顶级数据。
currentUser.updateProfile({
photoURL: currentUser.providerData[indexOfGitHub].photoURL
}
| 归档时间: |
|
| 查看次数: |
779 次 |
| 最近记录: |