benjanisa
benjanisa

Reputation: 96

IOS swift firebase auth incorrect log in

I have set up a log in scene using Firebase Auth. On entering correct details, the scene should change by performing the "loggedIn" segue. On entering incorrect details, the error should print to the console and nothing should happen.

I have found that despite picking up the error (and printing it to the console), the controller still performs the segue and moves to the next screen (however it doesn't print "LOGGING IN" to the console). Finding this behaviour very strange, any ideas?

I've copied my log in view controller below:

import Foundation
import Firebase
import FBSDKCoreKit
import FBSDKLoginKit

class LogInViewController: UIViewController{
@IBOutlet weak var Email: UITextField!
@IBOutlet weak var Password: UITextField!

@IBAction func Login(_ sender: AnyObject) {
    let email = self.Email.text!
    let password = self.Password.text!

    if (email != "" && password != ""){
        FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: { (user, error) in
            if(error==nil){
                print("LOGGING IN")
                self.performSegue(withIdentifier: "loggedIn", sender: nil)
            }
            else{
                print("COULDNT LOG IN")
                print(error)
            }
        })
    }
    else{
        let alert = UIAlertController(title: "Error", message: "Enter email and password", preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.addAction(action)
        self.present(alert, animated: true, completion: nil)
    }
}
}

Storyboard

EDIT

Storyboard with navigation controller and segues removed

I have added the Storyboard ID loggedInViewControllerVC_ID to the mealViewController in the identity inspector.

This is my new code:

import Firebase
import FBSDKCoreKit
import FBSDKLoginKit

class LogInViewController: UIViewController{

@IBOutlet weak var Email: UITextField!
@IBOutlet weak var Password: UITextField!
@IBAction func Login(_ sender: AnyObject) {
    let email = self.Email.text!
    let password = self.Password.text!

    if (email != "" && password != ""){
        FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: { (user, error) in
            if(error==nil){
                print("LOGGING IN")
                let nextViewController = self.navigationController?.storyboard?.instantiateViewController(withIdentifier: "loggedInViewControllerVC_ID") as! MealViewController
                self.navigationController?.pushViewController(nextViewController, animated: true)
                //self.performSegue(withIdentifier: "loggedIn", sender: nil)
            }
            else{
                print("COULDNT LOG IN")
                print(error)
            }
        })
    }
    else{
        let alert = UIAlertController(title: "Error", message: "Enter email and password", preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.addAction(action)
        self.present(alert, animated: true, completion: nil)
    }
}
}

I am now getting a BAD_INSTRUCTION error on successfully entering login details

Upvotes: 1

Views: 700

Answers (1)

Dravidian
Dravidian

Reputation: 9945

If you have connected your button in your storyboard to LoogedIn viewController via segue. It will segue the UI. What you can do is instantiate your viewController and then segue back and forth.

Delete that segue from storyboard.

 let loggedInScene = self.navigationController?.storyboard?.instantiateViewController(withIdentifier: "loggedInViewVontrollerVC_ID") as! loggedInViewVontroller 

 self.navigationController?.pushViewController(loggedInScene, animated: true)

PS:- loggedInViewVontrollerVC_ID is your storyboardID for the loggedIn viewController

Select the viewController you want to segue to in your storyboard -> Identity Inspector->StoryBoard ID -> loggedInViewVontrollerVC_ID.

And make sure that you have embed in the Navigation Controller in your first viewController.

Upvotes: 1

Related Questions