首次使用电子邮件登录后,强制更新Firebase用户个人资料照片

时间:2017-06-12 00:53:28

标签: ios swift firebase firebase-authentication

为了在我的测试版应用中预先注册某人,我通过电子邮件将其添加到我的Firebase Auth用户。一个他们第一次打开应用程序他们获得登录挑战(到目前为止一切都很好)。他们使用" Google Signin"按钮并使用相同的电子邮件地址。 Firebase允许他们进入,并使用我之前预先创建的相同身份验证用户。 (还是很棒。)

问题是:当用户使用" Google登录"他们的名字和照片不会添加到他们的个人资料中。请注意,如果他们使用" Google登录"首先,我会抓住额外的个人资料信息,这似乎是" Google登录"有效,但我似乎无法获得显示名称和照片。

有没有办法强制刷新个人资料信息,以便我可以提取他们的显示名称和照片?

(这是我在Firebase中手动添加电子邮件后使用的基本登录信息" Dashboard")

basic login

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {

    NotificationCenter.default.post(name: Notification.Name(CustomNotifications.AppLoginStart), object: nil)

    if let error = error {
        print("Error while Google Sign In - \(error)")
        NotificationCenter.default.post(name: Notification.Name(CustomNotifications.AppLoginFailure), object: nil)
        return
    }

    // Google Authorisation
    guard let authentication = user.authentication else { return }
    let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                   accessToken: authentication.accessToken)

    // Firebase Authorisation
    Auth.auth().signIn(with: credential) { (user, error) in

        if let error = error {
            print("Error while Firebase Auth - \(error)")
            NotificationCenter.default.post(name: Notification.Name(CustomNotifications.AppLoginFailure), object: nil)
            return
        }

        // Success
        print("Firebase Login Successful.")


        print("Firebase Login.", user!, user!.displayName ?? "No display Name")

        FirebaseHelper.sharedInstance.updateUserDetails(uid: user!.uid, email: user!.email!, name: user!.displayName, avatar: user?.photoURL)

        NotificationCenter.default.post(name: Notification.Name(CustomNotifications.AppLoginSucess), object: nil)
    }
}

2 个答案:

答案 0 :(得分:1)

主要问题是您必须拥有多个提供商。 默认情况下,用户对象具有有关第一个经过身份验证的提供程序的信在您的情况下,电子邮件提供商。

因此,当用户使用facebook登录时,配置文件没有更新这些详细信息,但详细信息(imageURL,Name,...)将添加到providerData这是一组提供者数据

因此,您需要获取providerData中的最后一个条目或过滤掉 providerIdhttps://firebase.google.com/docs/auth/web/manage-users#get_a_users_provider-specific_profile_information

答案 1 :(得分:0)

您是否尝试在登录后调用reload()上的FIRUser方法?

  

-reloadWithCompletion:

     

从服务器重新加载用户的个人资料数据。

     

来源:    https://firebase.google.com/docs/reference/ios/firebaseauth/api/reference/Classes/FIRUser#-reloadwithcompletion