如何快速从Firebase控制台注销用户

如何快速从Firebase控制台注销用户

本文介绍了如何快速从Firebase控制台注销用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用SignUpViewController进行了注册-

I have signed up using a SignUpViewController -

  import UIKit
  import Firebase
  import FirebaseFirestore
  import FirebaseAuth

   class SignUpViewController: UIViewController {

   @IBOutlet weak var firstNameTextField: UITextField!

   @IBOutlet weak var lastNameTextField: UITextField!

   @IBOutlet weak var emailTextField: UITextField!

   @IBOutlet weak var passwordTextField: UITextField!

   @IBOutlet weak var signUpButton: UIButton!

   @IBOutlet weak var errorLabel: UILabel!


   override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    setUpElements()
   }

   func setUpElements() {

    // Hide the error label
    errorLabel.alpha = 0

    // Style the elements
    Utilities.styleTextField(firstNameTextField)
    Utilities.styleTextField(lastNameTextField)
    Utilities.styleTextField(emailTextField)
    Utilities.styleTextField(passwordTextField)
    Utilities.styleFilledButton(signUpButton)
    }

   // Check the fields and validate that the data is correct. If everything is correct, this    method returns nil. Otherwise, it returns the error message
func validateFields() -> String? {

    // Check that all fields are filled in
    if firstNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        lastNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
        passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {

        return "Please fill in all fields."
    }

    // Check if the password is secure
    let cleanedPassword = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)

    if Utilities.isPasswordValid(cleanedPassword) == false {
        // Password isn't secure enough
        return "Please make sure your password is at least 8 characters, contains a special character and a number."






    }

    return nil
}
@IBAction func signUpTapped(_ sender: Any) {
    print("y")
    Analytics.logEvent(AnalyticsEventSignUp, parameters: nil)
    // Validate the fields
    let error = validateFields()

    if error != nil {

        // There's something wrong with the fields, show error message
        showError(error!)
    }
    else {

        // Create cleaned versions of the data
        let firstName = firstNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let lastName = lastNameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        let password = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
        // Create the user


        Auth.auth().createUser(withEmail: email, password: password) { authResult, error in


            // Check for errors
            if error != nil {

                // There was an error creating the user
                self.showError("Error creating user")
            }
            else {

                // User was created successfully, now store the first name and last name
                let db = Firestore.firestore()

                 db.collection("users").addDocument(data: ["email" : email, "firstname" :firstName, "lastname":lastName, "uid": authResult!.user.uid ])  { (error) in

                    if error != nil {
                        // Show error message
                        self.showError("Error saving user data")
                    }
                }

                // Transition to the home screen
                self.transitionToHome()
            }

        }



    }
}

func showError(_ message:String) {

    errorLabel.text = message
    errorLabel.alpha = 1
}

func transitionToHome() {

    let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController

    view.window?.rootViewController = homeViewController
    view.window?.makeKeyAndVisible()

    }

  }

上面的SignUpViewController可以正常工作,并且我在Firebase控制台中注册(自动登录),如下所示-

The above SignUpViewController works fine and I signup (which automatically signsin) in the Firebase console as shown below -

现在,当我尝试使用下面的LoginViewController进行登出/注销时,firebase控制台不会掉开眼皮:-

Now, when I try to signout/logout using LoginViewController below, the firebase console does not bat an eyelid :-

  import UIKit
  import FirebaseAuth
  class LoginViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var loginButton: UIButton!
@IBOutlet weak var errorLabel: UILabel!



  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    setUpElements()
 }

  func setUpElements() {

    // Hide the error label
    errorLabel.alpha = 0

    // Style the elements
    Utilities.styleTextField(emailTextField)
    Utilities.styleTextField(passwordTextField)
    Utilities.styleFilledButton(loginButton)

   }




     @IBAction func loginTapped(_ sender: Any) {

    // TODO: Validate Text Fields

    // Create cleaned versions of the text field
    let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
    let password = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)

    // Signing in the user
    Auth.auth().signIn(withEmail: email, password: password) { (result, error) in

        if error != nil {
            // Couldn't sign in
            self.errorLabel.text = error!.localizedDescription
            self.errorLabel.alpha = 1
          }
         else {

            let homeViewController = self.storyboard?.instantiateViewController(identifier:  Constants.Storyboard.homeViewController) as? HomeViewController

            self.view.window?.rootViewController = homeViewController
            self.view.window?.makeKeyAndVisible()
           }
        }
     }

      @IBAction func logoutTapped(_ sender: Any){  let firebaseAuth = Auth.auth()
    do {
    print("button pressed")
    Auth.auth().addStateDidChangeListener { auth, user in
    if User.self as Any? != nil {
    print("User is signed in.")
        do {
           try firebaseAuth.signOut()
           } catch let signOutError as NSError {
           print ("Error signing out: %@", signOutError)
           }
       } else {
    print("User is signed out.")
      }
     }


   }
  }

 }

LoginViewController屏幕截图-

LoginViewController screenshot -

那么,如何从Firebase控制台注销/注销?

So, how to logout/signout from the firebase console ?

所以,我有以下问题-

i)在LoginViewController中-如何在Firebase控制台中从LoginViewController注销/注销以及重新登录?

i)In LoginViewController - How to log-out/sign-out and also sign-back-in from LoginViewController in the firebase console ?

ii)在SignUpViewController中-如何阻止用户从SignUpViewController自动登录并改为通过LoginViewController进行登录?另外,在firestore中,没有集合用户"被收集.和文档电子邮件",名字",姓氏".我是否必须在Frestore中手动添加它们?

ii)In SignUpViewController - How to stop user from automatically signing in from SignUpViewController and make him/her sign-in through LoginViewController instead ? Also, in firestore, no collection "users" and documents "email", "firstname", "lastname". Do I have to add them manuallyl in Frestore ?

推荐答案

来自android背景,但退出用户应该很简单.也许试试这个

Coming from the android background but signing out the user should be straight forward. Maybe try this

do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print ("Error signing out: %@", signOutError)
}

这篇关于如何快速从Firebase控制台注销用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:35