Facebook Login Error "User cancelled Dialog" - 4201

Hi, I’m getting an error when trying to log in to Facebook. With the same problem many people have encountered and tried everything I can see. But I did not succeed.

error :
{errorCode: “4201”, errorMessage: “User cancelled dialog”}

code :
this.fb.login([‘public_profile’, ‘user_friends’, ‘email’])
.then((res: FacebookLoginResponse) => console.log(‘Logged into Facebook!’, res))
.catch(e => console.log(‘Error logging into Facebook’, e));

I had the same problem in my project as well. In my case it was because the user was already logged into Facebook via my app. I solved it by always making sure to logout of Facebook on logout

this.facebook.logout().then(() => { }).catch(() => { });

You can also check to see if the user is already logged in before attempting the login again:

this.facebook.getLoginStatus().then((res) => {
    if (res.status === 'connected') {
        // Already logged in to FB so pass credentials to provider (in my case firebase)
        let provider = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
        firebase.auth().signInWithCredential(provider).then((authToken) => {
           this.authToken = authToken;
        });
    } else {
        // Not already logged in to FB so sign in
        this.facebook.login([]).then((userData) => {
            // FB Log in success
        }).catch((error) => {
            // FB Log in error
        });
    }
};
5 Likes

Thank you very much, look for the solution and this resolved!

how to resolved

i have same error.

I had the same error and it was because when I added the facebook native plugin I added it like:

ionic cordova plugin add cordova-plugin-facebook4 --variable APP_ID="123456789" --variable APP_NAME="myApplication"

Changed the values in the config.xml but it didn’t took effect. I had to remove and add it again with the correct APP_ID (that was wrong, found the problem after uninstalling the facebook and the web popup told me the APP_ID was unusual. After this it worked :partying_face:

In case you seek the remove command:

ionic cordova plugin remove cordova-plugin-facebook4 
1 Like

This work for me! thanks