Facebook login : Make email mandatory

6.4k Views Asked by At

We are using facebook for login in our website.
We need email as a mandatory permission. If the user doesn't grant email permission then the dialog should give an alert() message saying 'Please grant email permission' and repeat.

I have seen this feature in few of the shopping sites. The below code works fine but doesn't have the feature I mentioned.
Help needed.

  window.fbAsyncInit = function () {
            FB.init({
                appId: 'MY APP ID',
                oauth: true,
                status: false, // check login status
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: true // parse XFBML
             });
    };

    function fb_login() {
        FB.login(function (response) {
            if (response.authResponse) {
                access_token = response.authResponse.accessToken; //get access token
                user_id = response.authResponse.userID; //get FB UID

                FB.api('/me', function (response) {
                    // you can store this data into your database
                    alert(response.email);
                });

            }
        }, {
            scope: 'email'
        });
    }
    (function () {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    } ());

Somewhere in the body of HTML.

<div class="span"><a onclick="fb_login();"><img src="css/images/facebook.png" alt=""/><sm>Sign In with Facebook</sm><div class="clear"></div></a></div> 

1

There are 1 best solutions below

0
Tobi On BEST ANSWER

Have a look at

especially the "Handling Missing Permissions" part of the first link.

When an app requests permissions, people may completely deny those permissions, not fully grant those permissions, or change them later. In order to provide a great experience, apps should be built to handle these situations.

  • First, apps should be able to handle any permissions that were requested but not granted:
  • Check granted permissions before trying to use APIs that require them. Detect permissions errors returned when an API request is made without the correct permission.