Firebase发布了两个不同的uid的-Swift iOS [英] Firebase issuing out 2 different uid's -Swift iOS

查看:251
本文介绍了Firebase发布了两个不同的uid的-Swift iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的所有豆荚都已正确安装。我的应用程序启动,然后当我到达必须访问Firebase的场景时,我注意到有一个崩溃。原因似乎是Firebase发给我两个不同的uid,而Auth中的那个与数据库中的不一致。



当我的App在AppDelegate I有代码获得当前的uid。

$ $ p $ //这是在App Delegate
let myUserID = FIRAuth.auth( )?currentUser?.uid

print(App Delegate:UID is \(myUserID!))
//示例打印为'App Delegate:UID is Abcdefg12345'

接下来会显示注册场景,并在该视图控制器中显示

  FIRAuth.auth()?。createUserWithEmail(self.emailTextField.text !,密码:self.passwordTextField.text !,完成:{

//你可以通过用户通过用户访问新发布的用户id
(user,err)
$ b print(SignUp:UID is \(user!uid)\ n)
//示例打印是'SignUp:UID是Vwxyz67890'

我访问用户!使用(从使用r)我从AppDelegate中得到了完全不同的uid。



然而,我从FIRAuth.auth()中获得的用户名是什么?createUserWithEmail是什么目前在Auth中,但在FirebaseDatabase中无法识别。



奇怪的是,在AppDelegate中打印的uid是FirebaseDatabase中的当前内容,但在Auth中无法识别。如果这个uid在注册时发生了变化,那么它不应该在数据库中可用。

我也有一个注销场景,当我按下注销按钮时,我得到注销uid如同在FIRAuth.auth()中创建的一样?createUserWithEmail



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



AppDelegate


$ b

  import UIKit 
导入Firebase
导入FirebaseAuth
导入FirebaseDatabase
导入FirebaseStorage
导入GoogleMaps

@UIApplicationMain $ b $ class AppDelegate:UIResponder,UIApplicationDelegate {

var window:UIWindow?

覆盖init(){
super.init()

//为FireBase配置应用程序
FIRApp.configure()

// Firebase App Offline更新
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

如果让boolean = connected.value as?Bool其中boolean == true {
print(\ nApp Delegate:Firebase is connected)

if myUserID!= nil {
//这个uid与signUp中创建的不同,但是它显示在数据库但不是验证
print(App Delegate:UID is \(myUserID!)\\\

}
} else {
print(\\\
App委托:Firebase未连接\\)
}
})

self.window?.makeKeyAndVisible()

返回true

SignUpController



$ p $ 导入UIKit
导入Firebase
导入FirebaseAuth
导入FirebaseDatabase
导入FirebaseCrash

class SignUpController:UIViewController {

@IBOutlet weak var emailTextField:UITextField!
@IBOutlet弱变量passwordTextField:UITextField!

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


重写func viewDidLoad(){
super.viewDidLoad()
self .dbRef = FIRDatabase.database()。reference()
}

@IBAction func signUpButton(sender:UIButton){

FIRAuth.auth()?. createUserWithEmail(self.emailTextField.text !,密码:self.passwordTextField.text!,完成:
$ b(user,err)in

//这个uid与AppDelegate但在Auth中显示,但不在数据库中
print(\\\
SignUp:UID is \(user!uid)\\\

print(现在查看UID从上面的属性myUserID:\(self.myUserID))

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

}

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

让email = self.self.emailTextField.text!
让字典= [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

 导入UIKit 
导入Firebase
导入FirebaseAuth
导入FirebaseDatabase
导入FirebaseCrash

类LogoutController:UIViewController {

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


重写func viewDidLoad(){
super.viewDidLoad()
self .dbRef = FIRDatabase.database()。reference()
}

$ b @IBAction func signUpButton(sender:UIButton){

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

print(\\\
Logout:UID is \(self.myUserID!))

}
}


解决方案

应用程序委托,而不检查用户是否登录。它可能会为用户分配一个临时/匿名帐户,直到他们创建一个帐户。检查Firebase控制台中的Auth设置,看看是否有多个帐户正在创建。



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

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

  }
}

解决方案

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

这篇关于Firebase发布了两个不同的uid的-Swift iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆