Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have all my pods correctly installed. My app launches and then when I get to scenes that have to access Firebase I've noticed there's a crash. The reason seems to be that Firebase is issuing me 2 different uid's and the one in Auth doesn't match the one in the Database.

When my app launches in AppDelegate I have code to get the current 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'

Next a SignUp scene is presented and in that view controller I have

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)
")
//An example print is 'SignUp: UID is Vwxyz67890'

When I access user!uid (from user) I get a totally different uid from what is originally in the AppDelegate.

However the uid that I get from user in FIRAuth.auth()?.createUserWithEmail is what's current in Auth but is not recognized in FirebaseDatabase.

The odd thing is the uid that is printed in AppDelegate is what's current in the FirebaseDatabase but is not recognized in Auth. If this uid is changed upon signing up then it should not be available in the Database

I also have a logout scene and when I press the logout button I get the logout uid as the same one created in FIRAuth.auth()?.createUserWithEmail

Any ideas why I have 2 different uid's? This seems to be my crash issues.

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("
App 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!)
")
                }
            } else {
                print("
App Delegate: Firebase is NOT connected
")
            }
        })

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("
SignUp: UID is (user!uid)
")
print("Now Look at the UID printed from the above property myUserID: (self.myUserID)")

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

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

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("
Logout: UID is (self.myUserID!)")

  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
134 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...