(iOS + Facebook Auth)无法生成UID并将用户详细信息保存在Firebase中

时间:2018-12-19 08:05:49

标签: ios swift facebook firebase firebase-authentication

我有一个iOS项目,该项目可以通过Google,Email或Facebook验证用户身份。用户通过身份验证后,firebase会自动将用户添加到Firebase控制台上的“身份验证用户”列表中。我还将用户详细信息(电子邮件,UID)保存在我的实时数据库中的root / User下。

我的Facebook登录有效,但是没有打到我的Firebase项目(即:未在控制台中显示经过身份验证的用户等)。我需要用户出现在这里,所以我可以使用UID链接数据等。

这是我的AppDelegate.swift

import UIKit
import Firebase
import GoogleSignIn
import FBSDKLoginKit
import FacebookCore

var fbAccessToken: AccessToken?


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        // Use Firebase library to configure APIs
        FirebaseApp.configure()

        // Facebook
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)




        // Added from Firebase docs -- Google
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
        GIDSignIn.sharedInstance().delegate = self

//        if currentUser != nil {
//            self.window?.rootViewController?.performSegue(withIdentifier: "loginPush", sender: nil)
//        }

        return true
    }

    @available(iOS 9.0, *)
    func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        // FB Auth
        let handled: Bool = SDKApplicationDelegate.shared.application(application, open: url,  options: options)

        if !handled{
        // Google Auth
            print("clicked google sign in")
            return GIDSignIn.sharedInstance().handle(url,
                                                     sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
                                                     annotation: [:])
        }
        return true
    }

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error{
            print(error.localizedDescription)
            return
        } else {
            guard let authentication = user.authentication else { return }
            let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                           accessToken: authentication.accessToken)
            Auth.auth().signInAndRetrieveData(with: credential) { (result, error) in
                if error == nil{
                    print(result?.user.email)
                    self.window?.rootViewController?.performSegue(withIdentifier: "loginPush", sender: nil)

                    self.checkUser()
                } else {
                    print(error?.localizedDescription)
                }
            }
        }
    }

    func checkUser(){
        guard let uid = Auth.auth().currentUser?.uid else {return}
        Database.database().reference().child("User/\(uid)").observeSingleEvent(of: .value) { (snapshot) in
            if snapshot.exists(){
                print("User exists")
            } else {
                print("Path doesn't exist")
                // Add the user to the database
                let userData = [
                    uid: Auth.auth().currentUser?.email!
                    ] as [String : Any]
                Database.database().reference().child("User/\(uid)").setValue(userData)
            }
        }
    }

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

    func applicationWillResignActive(_ application: UIApplication) {
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        FBSDKAppEvents.activateApp()
    }

    func applicationWillTerminate(_ application: UIApplication) {
    }
}

我的ViewController.swift上没有直接影响Facebook身份验证的代码,当我尝试添加@IBAction func从Facebook保存UID等时,该按钮将无法正常工作(即, :点击后,它将返回到原始状态)

0 个答案:

没有答案