【问题标题】:Post First & Last name from Gmail account to Firebase将名字和姓氏从 Gmail 帐户发布到 Firebase
【发布时间】:2017-06-17 10:22:40
【问题描述】:

当通过 firebaseAuth 完成身份验证时,如何将姓名从 Gmail 帐户发布到 firebase。?如何获取用户资料?

我正在通过 GIDGoogleUser.. 在 Firebase 中创建用户并使用:

let authentication = user.authentication

我在同一个班级发现了一些我可能可以使用的东西,但我需要建议它是正确的方法以及如何从中获取名字和姓氏..?

let profile = user.profile

我的 AppDelegate.swift

import UIKit
import Firebase

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
  var window: UIWindow?
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    FirebaseApp.configure()

    GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
    GIDSignIn.sharedInstance().delegate = self

    UIApplication.shared.statusBarStyle = .lightContent
  }
  func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    if let error = error {
        print(error.localizedDescription)
        return
    }
    let authentication = user.authentication
    let credential = GoogleAuthProvider.credential(withIDToken (authentication?.idToken)!,accessToken: (authentication?.accessToken)!)

    Auth.auth().signIn(with: credential) { (user, error) in  
        if error != nil {
            return
        } else {
            let registrationReq = FirebaseRegistrationLoginRequests()
            registrationReq.checkIfCreateOrGetUser(user: user!)
        }
    }
  }

  func sign(_ signIn: GIDSignIn!, didDisconnectWith user:GIDGoogleUser!, withError error: Error!) {
    // Perform any operations when the user disconnects from app here.
    // ...
  }
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {     
    debugPrint(userInfo) 
    completionHandler(UIBackgroundFetchResult.newData)
  }
}

【问题讨论】:

    标签: ios swift firebase firebase-authentication


    【解决方案1】:

    我想通了。

    添加后

    let authentication = user.authentication
    

    这个:

    let fullName = user.profile.name
    let firstName = user.profile.givenName
    let lastName = user.profile.familyName
    

    比在:

        Auth.auth().signIn(with: credential) { (user, error) in
    
            debugPrint("fullName:", fullName!)
            debugPrint("firstName:", firstName!)
            debugPrint("lastName:", lastName!)
    
            if error != nil {
                return
            } else {
                let registrationReq = FirebaseRegistrationLoginRequests()
                registrationReq.checkIfCreateOrGetUser(user: user!)
            }
    
        }
    

    您还可以获得很多其他详细信息:

                 Get the account information you want here from the dictionary
                 Possible values are
                 "id": "...",
                 "email": "...",
                 "verified_email": ...,
                 "name": "...",
                 "given_name": "...",
                 "family_name": "...",
                 "link": "https://plus.google.com/...",
                 "picture": "https://lh5.googleuserco...",
                 "gender": "...",
                 "locale": "..."
    

    【讨论】:

      【解决方案2】:

      表单文档here:

      GIDGoogleUser 有一个名为 profile 的属性,它类似于 GIDProfileData

      GIDProfileData 有您需要的信息。

      here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-13
        • 1970-01-01
        • 2016-02-22
        • 1970-01-01
        • 1970-01-01
        • 2019-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多