firebase-ios-sdk icon indicating copy to clipboard operation
firebase-ios-sdk copied to clipboard

linkAndRetrieveData with Facebook credential fails to retrieve displayName

Open joelmarquez90 opened this issue 8 years ago • 5 comments

[REQUIRED] Step 2: Describe your environment

  • Xcode version: 9.4.1
  • Firebase SDK version: FirebaseCore 5.1.0
  • Firebase Component: Auth
  • Component version: FirebaseAuth 5.0.3

[REQUIRED] Step 3: Describe the problem

If I have an anonymous user and then I login with facebook and use the linkAndRetrieveData, the displayName property of the user is nil.

Steps to reproduce:

What happened? How can we make the problem occur? This could be a description, log/console output, etc.

If I have an anonymous user and then I login with facebook and use the linkAndRetrieveData, the displayName is nil. Checking with the Graph Explorer tool of Facebook and with the retrieved access token from the Facebook SDK I can retrieve my full name.

However, if I use the signInAndRetrieveData from the Auth component, I can retrieve it:

Relevant Code:

Doesn't work
let facebookCredential = FacebookAuthProvider.credential(withAccessToken: facebookAccessToken)
user.linkAndRetrieveData(with: facebookCredential, completion: { result, error in
  if let user = result?.user {
    print(result?.user?.displayName ?? "No display name") // No display name is printed
  }
})
Works
let facebookCredential = FacebookAuthProvider.credential(withAccessToken: facebookAccessToken)
Auth.auth().signInAndRetrieveData(with: facebookCredential, completion: { result, error in
  if let user = result?.user {
    print(result?.user?.displayName ?? "No display name") // Joel Márquez is printed
  }                  
})

joelmarquez90 avatar Aug 15 '18 18:08 joelmarquez90

could you please see if it return with an error?

I could reproduce this issue with a merge conflict case. In this case, an FUIAuthErrorCodeMergeConflict error would be thrown from firebaseUI. In this case, the call won't succeed and all the user info would be nil (because result is nil).

But it does have a valid credential after a successful login, so it will eventually update all the user information with a cache refresh.

Yue-Wang-Google avatar Aug 17 '18 00:08 Yue-Wang-Google

@Yue-Wang-Google no, there's no error on the callback.

In fact, debugging the Firebase iOS SDK code, it seems that at some point is throwing the full name from Facebook.

At this point:

screen shot 2018-08-18 at 17 23 00

The object has this information:

screen shot 2018-08-18 at 17 22 45

And in the last part of the method:

            FIRGetAccountInfoRequest *getAccountInfoRequest =
                [[FIRGetAccountInfoRequest alloc] initWithAccessToken:accessToken
                                                 requestConfiguration:requestConfiguration];
            [FIRAuthBackend getAccountInfo:getAccountInfoRequest
                                  callback:^(FIRGetAccountInfoResponse *_Nullable response,
                                             NSError *_Nullable error) {
              if (error) {
                [self signOutIfTokenIsInvalidWithError:error];
                completeWithError(nil, error);
                return;
              }
              self->_anonymous = NO;
              [self updateWithGetAccountInfoResponse:response];

Inside the [self updateWithGetAccountInfoResponse:response];, the response has the following:

screen shot 2018-08-18 at 17 23 42

It's weird. If you need more information, I'm glad to give it to you.

joelmarquez90 avatar Aug 18 '18 14:08 joelmarquez90

@joelmarquez90 Hi Joel, this is an expected behavior.

  1. When signInAndRetrieveData is called, a new user will be generated and the user info will be updated from user provider info automatically (Facebook in your case).
  2. When linkAndRetrieveData is called, the user provider info will be linked with the current user as providerUserInfo, but will not override user info (displayName in your case). The reason is that a user can link multiple providers and their displayName can be different, so you need to choose and update it yourself.

renkelvin avatar Sep 24 '18 18:09 renkelvin

Thanks @renkelvin for clearing up that.

joelmarquez90 avatar Sep 26 '18 03:09 joelmarquez90

Just for anyone there that steps up with this problem, the solution was to check for the providerData first object, and then grabbing the displayName on it, which was the Facebook user name.

joelmarquez90 avatar Dec 02 '18 01:12 joelmarquez90