问题
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)\n")
//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("\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!)")
}
}
回答1:
You're asking firebase for a uid in the app delegate without checking if a user is logged in. It may be assigning a temporary/anonymous account to the user until they create an account. Check the "Auth" settings in the firebase console and see if multiple accounts are being created in the process.
Remove the code from the App Delegate and move "let myUserID = FIRAuth.auth()?.currentUser?.uid" into the 'createUserWithEmail' function in the signup controller
回答2:
Problem solved. Here's the new code via @Justin Doan 's accepted answer
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")
}
})
})
}
}
There's no change to LogoutController
来源:https://stackoverflow.com/questions/39553291/firebase-issuing-out-2-different-uids-swift-ios