Facebook Login Error "User cancelled Dialog" - 4201

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