Firebase发布2个不同的uid&#39s -Swift iOS

时间:2016-09-18 01:38:50

标签: ios swift firebase-realtime-database firebase-authentication uid

我已正确安装了所有pod。我的应用程序启动,然后当我到达必须访问Firebase的场景时,我注意到那里发生了崩溃。原因似乎是Firebase向我发出了2个不同的uid,而Auth中的那个并没有与数据库中的那个匹配。

当我的应用程序在AppDelegate中启动时,我有代码来获取当前的uid。

//This is in App Delegate
let myUserID = FIRAuth.auth()?.currentUser?.uid

print("App Delegate: UID is \(myUserID!)")
//An example print is 'App Delegate: UID is Abcdefg12345'

接下来会出现一个SignUp场景,在那个视图控制器中我有

FIRAuth.auth()?.createUserWithEmail(self.emailTextField.text!, password: self.passwordTextField.text!, completion: {

            //You can access the newly issued uid from user via user!uid
            (user, err) in

print("SignUp: UID is \(user!uid)\n")
//An example print is 'SignUp: UID is Vwxyz67890'

当我访问user!uid(来自用户)时,我得到了与AppDelegate中最初的uid完全不同的内容。

但是我在FIRAuth.auth()?createSuserWithEmail中从用户获取的uid是Auth中的当前内容,但在FirebaseDatabase中无法识别。

奇怪的是,在AppDelegate中打印的uid是FirebaseDatabase中的当前内容,但在Auth中无法识别。如果在注册时更改了此uid,则它不应在数据库中可用

我还有一个注销场景,当我按下注销按钮时,我得到了注销uid,就像在FIRAuth.auth()中创建的那个一样?。createUserWithEmail

为什么我有2个不同的uid?这似乎是我的崩溃问题。

的AppDelegate

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
import GoogleMaps

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    override init() {
        super.init()

        //Configure App for FireBase
        FIRApp.configure()

        //Firebase App Offline Updates
        FIRDatabase.database().persistenceEnabled = true

        GMSServices.provideAPIKey("mykey")
    }//end override init


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let myUserID = FIRAuth.auth()?.currentUser?.uid

        let connectedRef = FIRDatabase.database().referenceWithPath(".info/connected")
        connectedRef.observeEventType(.Value, withBlock: { 
                    (connected) in

        if let boolean = connected.value as? Bool where boolean == true {
                print("\nApp Delegate: Firebase is connected")

                if myUserID != nil{
                //This uid is different from the one created in signUp but yet it is what's shown in the Database but not Auth
                print("App Delegate: UID is \(myUserID!)\n")
                }
            } else {
                print("\nApp Delegate: Firebase is NOT connected\n")
            }
        })

self.window?.makeKeyAndVisible()

        return true
    }

SignUpController

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseCrash

class SignUpController: UIViewController {

@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!

var dbRef: FIRDatabaseReference!
let myUserID = FIRAuth.auth()?.currentUser?.uid


override func viewDidLoad() {
      super.viewDidLoad()
self.dbRef = FIRDatabase.database().reference()
}

@IBAction func signUpButton(sender: UIButton) {

FIRAuth.auth()?.createUserWithEmail(self.emailTextField.text!, password: self.passwordTextField.text!, completion: {

            (user, err) in

//This uid is different from AppDelegate but is what is shown in Auth but not in Database
print("\nSignUp: UID is \(user!uid)\n")
print("Now Look at the UID printed from the above property myUserID: \(self.myUserID)")

if err != nil{
        print((error?.localizedDescription)\n)
       }

let usersRef = self.dbRef.child("users")
let userIDRef = usersRef.child(self.myUserID!)
let userEmailRef = userIDRef.child("userEmailRef")

let email = self.self.emailTextField.text!
let dict = [String:AnyObject]()
dict.updateValue(email, forKey: "emailKey")
dict.updateValue(self.myUserID!, forKey: "userIDKey")

userEmailRef.updateChildValues(dict, withCompletionBlock: {

                (error, user) in

                if error != nil{
                    print(err?.localizedDescription\n")
                }
     })
   })
 }
}

LogoOutController

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseCrash

class LogoutController: UIViewController {

var dbRef: FIRDatabaseReference!
let myUserID = FIRAuth.auth()?.currentUser?.uid


override func viewDidLoad() {
      super.viewDidLoad()
self.dbRef = FIRDatabase.database().reference()
}


@IBAction func signUpButton(sender: UIButton) {

try! FIRAuth.auth()!.signOut()

print("\nLogout: UID is \(self.myUserID!)")

  }
}

2 个答案:

答案 0 :(得分:1)

你要求firebase获取app委托中的uid,而不检查用户是否已登录。它可能会为用户分配一个临时/匿名帐户,直到他们创建一个帐户。检查firebase控制台中的“Auth”设置,看看是否在此过程中创建了多个帐户。

从App Delegate中删除代码并将“let myUserID = FIRAuth.auth()?。currentUser?.uid”移动到注册控制器中的“createUserWithEmail”函数

答案 1 :(得分:0)

问题解决了。这是通过@Justin Doan接受的答案的新代码

的AppDelegate

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
import GoogleMaps

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    override init() {
        super.init()

        //Configure App for FireBase
        FIRApp.configure()

        //Firebase App Offline Updates
        FIRDatabase.database().persistenceEnabled = true

        GMSServices.provideAPIKey("mykey")
    }//end override init


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let connectedRef = FIRDatabase.database().referenceWithPath(".info/connected")
        connectedRef.observeEventType(.Value, withBlock: { 
                    (connected) in

        if let boolean = connected.value as? Bool where boolean == true {
                print("\nApp Delegate: Firebase is connected")
            } else {
                print("\nApp Delegate: Firebase is NOT connected\n")
            }
        })

self.window?.makeKeyAndVisible()

        return true
    }

SignUpController

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseCrash

class SignUpController: UIViewController {

@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!

var dbRef: FIRDatabaseReference!

override func viewDidLoad() {
      super.viewDidLoad()
self.dbRef = FIRDatabase.database().reference()
}

@IBAction func signUpButton(sender: UIButton) {

FIRAuth.auth()?.createUserWithEmail(self.emailTextField.text!, password: self.passwordTextField.text!, completion: {

            (user, err) in

let myUserID = FIRAuth.auth()?.currentUser?.uid

//This should always print a successful match
if user!.uid != currentUserID{
            print("\nYOUR USER ID'S DON'T MATCH")
        }else{
            print("\nTHE USER ID's SUCCESSFULLY MATCH")
        }

if err != nil{
        print((error?.localizedDescription)\n)
       }

let usersRef = self.dbRef.child("users")
let userIDRef = usersRef.child(self.myUserID!)
let userEmailRef = userIDRef.child("userEmailRef")

let email = self.self.emailTextField.text!
let dict = [String:AnyObject]()
dict.updateValue(email, forKey: "emailKey")
dict.updateValue(self.myUserID!, forKey: "userIDKey")

userEmailRef.updateChildValues(dict, withCompletionBlock: {

                (error, user) in

                if error != nil{
                    print(err?.localizedDescription\n")
                }
     })
   })
 }
}

LogoutController没有变化