Firebase电子邮件登录和注册,无需检查错误即可登录

时间:2018-07-14 09:40:59

标签: swift firebase firebase-authentication

@IBAction func signup(_ sender: Any) {
    Auth.auth().createUser(withEmail: username.text!, password: password.text!) { (user, error) in
        if error != nil {
            print(error!)
        } else {
            self.performSegue(withIdentifier: "signup", sender: self.button)


        }
    }
}
 @IBAction func loginButtonPressed(_ sender: Any) {


    Auth.auth().signIn(withEmail: userName.text!, password: password.text!) { (user, error) in

        if let error = error {
           print(error)

        } else    {

             self.performSegue(withIdentifier: "start", sender: self.loginButton)
        }
    }

}

1 个答案:

答案 0 :(得分:0)

请尝试...

@IBAction func loginAction(_ sender: Any)
{
    if self.emailTextField.text == "" || self.passwordTextField.text == "" {

        //Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in

        let alertController = UIAlertController(title: "Error", 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 {

        Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in

            if error == nil {

                //Print into the console if successfully logged in
                print("You have successfully logged in")

                //Go to the HomeViewController if the login is sucessful
                let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                self.present(vc!, animated: true, completion: nil)

            } else {

                //Tells the user that there is an error and then gets firebase to tell them the error
                let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)

                self.present(alertController, animated: true, completion: nil)
            }
        }
    }
}