0

I am trying to utilize the login function, and when I switched to swift 4 I am now getting an error. the function crashes and gives a fatal error: unexpectedly found nil while unwrapping an Optional value error. However, when I run the print statements for the email and password passed it prints them out fine as non-optional strings.

Below is my code:

@IBAction func Login(sender: AnyObject) {
    let email = self._Email.text!
    let password = self._Password.text!
    print("below is the email")
    print(email)
    print("below is the password")
    print(password)
    FIRAuth.auth()!.signIn(withEmail: email, password: password, completion: { user, error in
        if error == nil {
            //successfull login
            print("Successful login****************************************")

            //performs a segue to the next view controller
            if user!.isEmailVerified{
                //if the email is verified
                let vc = self.storyboard!.instantiateViewController(withIdentifier: "ProfileView") as! ProfileView
                self.present(vc, animated: true, completion: nil)
            }
            else {
                if let errCode = FIRAuthErrorCode(rawValue: error!._code) {

                    switch errCode {
                    case .errorCodeNetworkError:
                        print("There has been a connection error")
                    case .errorCodeInvalidEmail:
                        print("invalid email")
                    case .errorCodeEmailAlreadyInUse:
                        print("in use")
                    default:
                        print("Create User Error: \(error!)")
                        //error with login
                        self.signupErrorAlert(title: "Error", message: "Email or password is not recognized")
                        print ("Login Error**************")
                    }
                }
            }
        }
    })
}

The exception happens on the following line: FIRAuth.auth()!.signIn(withEmail: email, password: password, completion: { user, error in

Drew
  • 1,341
  • 3
  • 13
  • 18
  • 1
    You need to say on which line of code the exception happens. – Mo Abdul-Hameed Oct 20 '17 at 22:52
  • @MoeAbdul-Hameed My apologies, I have added that in – Drew Oct 20 '17 at 23:05
  • The `!` operator is the "force unwrap" operator. I call it the "crash if nil" operator. It sounds like the auth() function can return nil, and then you force-unwrap it. If you get nil and then force-unwrap the nil, you will crash, guaranteed. Don't do that. – Duncan C Oct 21 '17 at 01:50

0 Answers0