使用电子邮件密码

时间:2017-03-18 11:58:34

标签: firebase swift3 firebase-realtime-database firebase-authentication

这是一个特别的而不是一般的'你怎么做....'问题。在我的应用程序上,用户可以使用电子邮件和密码登录到Firebase帐户,然后将其带到包含许多字段的数据输入页面。如果用户已经存在,他们的数据将下载到他们可以编辑的字段并保存回数据库。

所有这一切都很好,除了一件事。当我测试它并登录帐户时,它会从用户下载现有数据。但是,如果我注销并使用其他用户登录,它仍会下载(或显示)上次登录时的信息。我已检查用户实际使用" FIRAuth.auth()?addStateDidChangeListener&#34 ;.在特定设备上只有一个用户,这不是一个真正的问题,但我不喜欢它不能正常工作,解决它会很好。任何帮助将不胜感激 - 也许我错过了一些明显的东西? : - )

有相当多的代码,所以我把它减少了一点但保留了主要部分。 (小咆哮。我注意到很多时候人们要求更多的代码,他们往往不是那些真正继续帮助的人。我认为声誉建设者!非常讨厌)。

class UserLoginViewController: UIViewController {

@IBOutlet weak var userNameLabel: UILabel!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var logoutButton: UIButton!

var ref: FIRDatabaseReference!

override func viewDidLoad() {
super.viewDidLoad()

   ref = FIRDatabase.database().reference()

//======Check if logged in, if so hide login button
    if let user = FIRAuth.auth()?.currentUser
    {
        self.logoutButton.alpha = 1.0
        self.userNameLabel.text = user.email
    }
    else
    {
        self.logoutButton.alpha = 0.0
        self.userNameLabel.text = ""
    }
}

//======LogIn

@IBAction func loginAction(_ sender: AnyObject)
{
    if self.emailField.text == "" || self.passwordField.text == ""
    {
        let alertController = UIAlertController(title: "Oops!", message: "Please enter an email and password.", preferredStyle: .alert)         
        let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertController.addAction(defaultAction)

        self.present(alertController, animated: true, completion: nil)
    }
    else
    {
        FIRAuth.auth()?.signIn(withEmail: self.emailField.text!, password: self.passwordField.text!) { (user, error) in
            if error == nil
            {
                self.logoutButton.alpha = 1.0
                self.userNameLabel.text = user!.email
                self.emailField.text = ""
                self.passwordField.text = ""
        // ===Segue to DataInput
                self.performSegue(withIdentifier: "loginSegue",sender: nil)
            }
            else
            {
                let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .alert)
                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)
                self.present(alertController, animated: true, completion: nil)
            }
        }
    }
}

然后链接到数据输入页面:

import UIKit
import Firebase

var ref: FIRDatabaseReference!
let user = FIRAuth.auth()?.currentUser

var myEmptyField = ""
var myUid = user!.uid

class DataInputViewController:  UIViewController {

@IBAction func unwindDataInputViewController(segue: UIStoryboardSegue) {
}

var newAccount: String!
var myLon: String!
var myLat: String!


// ref user data on Firebase
let ownernameRef = FIRDatabase.database().reference().child("/users/\(myUid)/ownername")
let nameRef = FIRDatabase.database().reference().child("/users/\(myUid)/name")
let typeRef = FIRDatabase.database().reference().child("users/\(myUid)/type")

@IBOutlet weak var ownerNameTextField: UITextField!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var areaTextField: UITextField!
@IBOutlet weak var typeTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
 //   clearNull()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    //reads database to viewcontroller
    if newAccount == "no" {   

    self.ownernameRef.observe(.value) { (snap:FIRDataSnapshot) in self.ownerNameTextField.text = (snap.value as AnyObject).description}
    self.nameRef.observe(.value) { (snap:FIRDataSnapshot) in self.nameTextField.text = (snap.value as AnyObject).description}
    self.typeRef.observe(.value) { (snap:FIRDataSnapshot) in ion}

            }
    }else{
        if newAccount == "yes"{
            let date = Date()
            let formatter = DateFormatter()
            formatter.dateFormat = "dd.MM.yyyy"
            let startDate = formatter.string(from: date)

            accountSetupDateTextField.text = startDate

         //   clearNull()
        }
    }

1 个答案:

答案 0 :(得分:0)

我希望你能在五个月内解决问题,但也许其他人需要答案。

我今天遇到了同样的问题,我发现的唯一解决方案是:

  • set" let user = FIRAuth.auth()?currentUser"作为变量" var"
  • 更改" var myUid = user!.uid" to" var myUid = String()"
  • 您在UserLoginViewController的viewDidLoad方法中的
  • 放置" user = nil"
  • 在DataInputViewController的viewDidLoad方法中放入" user = FIRAuth.auth()?currentUser"并添加" myUid = user!.uid"

在此之后,所有工作都完美适合我!

希望我能帮助这个答案。