-1

I'm having difficulty with setting up a button that will take the user to another View Controller if the Firebase authentication is valid and the user and password match

@IBAction func loginButton(_ sender: Any) {
    if let email:String = Username.text, let pass:String = Password.text {
        FIRAuth.auth()?.signIn(withEmail: email, password: pass) { (user, error) in
            if let error = error {
                let errCode = FIRAuthErrorCode(rawValue: (error._code));

                if (errCode == .errorCodeUserNotFound) {
                    self.label.text = "Error: user not found";
                }
                else if (errCode == .errorCodeWrongPassword) {
                    self.label.text = "Error: wrong password";
                }
                else if (errCode == .errorCodeUserDisabled) {
                    self.label.text = "Error: User account disabled";
                }
                else {
                    self.label.text = error.localizedDescription;
                }
            }
            if let user = user {
                self.label.text = "Signed in as " + user.email!;

                self.Username.text = nil;
                self.Password.text = nil;
            }
Jballer
  • 29
  • 4
  • 2
    Not related but this is Swift: No semicolons, no parentheses around if conditions. – vadian May 01 '17 at 19:13
  • the completion handler is asynchronous - so the line 'if let user = user { ' will **always** be executed before you have set the label text. You need to move the check inside the completion handler – Russell May 01 '17 at 19:21
  • What difficulty, exactly, are you having? [Edit] your question with clear, specific details about your issue. – rmaddy May 01 '17 at 20:26
  • Possible duplicate of [How to switch views programmatically in a ViewController? (XCode iPhone)](http://stackoverflow.com/questions/10933939/how-to-switch-views-programmatically-in-a-viewcontroller-xcode-iphone) – Georgio Sayegh May 01 '17 at 22:55
  • This will help you: http://stackoverflow.com/questions/27604192/ios-how-to-segue-programmatically-using-swift – Andy Lebowitz May 02 '17 at 02:04

2 Answers2

0

The question is unclear and your code is not correct, in fact like @vadian mentioned swift doesn't have any semi-columns or the rest. But if you're intending on moving to a new view controller after validating login with firebase here is the ojective-c code to use:

[[FIRAuth auth] signInWithEmail:Username.text
                           password:Password.text
                         completion:^(FIRUser *user, NSError *error) {

                             if(error){
                                // do things related to error handling
                             } else{
                                 NSLog(@"You're logged in");
                                 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                                 YourViewController *viewController = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:@"Your Identifier"];
                                 [self presentViewController:viewController animated:YES completion:nil];
                             }

                         }];
Georgio Sayegh
  • 343
  • 1
  • 15
0

Swift 3.0

@IBAction func signIn(_ sender: Any) {
// log the user in
    FIRAuth.auth()?.signIn(withEmail: emailField.text!, password: passwordField.text!, completion: { user, error in
        if error == nil {
            // If There is no error with Login...
            print("Successfully Logged IN \(user!)")

            // Perform your segue...
            self.performSegue(withIdentifier: "signedIn", sender: self)
        }
    })
}

Add your segue between controllers, and call it 'signedIn' enter image description here

Tyler Rutt
  • 567
  • 2
  • 8
  • 24
  • Where do I specify the ViewController that I want the button to take the user to? and does this code completely replace the code that was written before? – Jballer May 02 '17 at 14:54
  • add a segue between your two controllers, (your Login one, and the one you want the successful login to take it to) and name it "signedIn". You can do this in the Storyboard – Tyler Rutt May 02 '17 at 15:07