Search code examples
swiftfirebasefacebook-authentication

Firebase and Facebook Authentication


I am trying to use Firebase and Facebook login and it is working, but when the Facebook page opens and you close it without logging in it throws an error and says "fatal error: unexpectedly found nil while unwrapping an Optional value" The line that is highlighted as causing the error is let credential.... Here is my code:

// Login button is pressed
    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

        if let error = error {

          print(error.localizedDescription)

        } else {

            let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
            FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in

                if let error = error {

                    print(error.localizedDescription)

                } else {

                    self.performSegueWithIdentifier("loginSegue", sender: self)
                    print("Login complete")

                }
            }
        }
    }

Can someone please help me? Thanks


Solution

  • You need to check also if you have a user, or probably the user canceled the sign up

    if let user = user {
    
     } else {
       print("Uh oh. The user cancelled the Facebook login."))
    }