Post First&从Gmail帐户到Firebase的姓氏

时间:2017-06-17 10:22:41

标签: ios swift firebase firebase-authentication

如何通过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)
  }
}

2 个答案:

答案 0 :(得分:0)

我想通了。

之后添加
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": "..."

答案 1 :(得分:0)

表单文档here

GIDGoogleUser具有名为profile的属性,类似于GIDProfileData类。

GIDProfileData包含您需要的信息。

here