2017-06-17 38 views
0

如何在通過firebaseAuth進行身份驗證時將Gmail帳戶的名字和姓氏發佈到firebase。如何獲取用戶配置文件?從Gmail帳戶向Firebase發佈名字和姓氏

我創建用戶在火力上GIDGoogleUser ..和使用:

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) 
    } 
} 

回答

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": "..." 
0

表格文檔here

GIDGoogleUser有一個名爲profile財產這是一種GIDProfileData類。

GIDProfileData有您需要的信息。

Look here