Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make email permission required when logging in/signing up a user using the FB login?

I can't seem to make the email permission required.

If a user unticks the email udring the FB login, my code breaks down. And when I try writing code around this problem (try to alert the user to accept the email permission requisite.), it won't work; the user would have to revoke the App's access manually in their FB settings, and that is obviously out of the question.

So how can I make the email permission required as part of my app/website?

Tbh, I couldn't read through the entire Facebook API docs without scratching my head through it.

Here is what I have currently:

    var $user = null;
    function __construct()
    {
           //blahblahlblah
           //blahblahblah (Code needed for facebook login to work)

           $this->user = $this->facebook->getUser();
           if ($this->user)
           {
              try
              {
                 // Proceed knowing you have a logged in user
                 $this->user = $this->facebook->api('/me');
                 $this->logoutUrl = $this->facebook->getLogoutUrl( array('next' => base_url() . 'connect/logout') );
              }
              catch (FacebookApiException $e)
              {
                 error_log($e);
                 $this->user = null;
              }
           }
    }
    function process()
    {
        // If the Facebook user allows his/her email to be shared.
        // [If they don't, skip to the `else` statement below]
        if (isset($this->user['email']))
        {
            //Great! I can process normally
        }
        // If the Facebook user doesn't share their email.
        else
        {
            //What do I do here?
            //I'm not sure how the OAuth works, so that's why im checking
            echo 'email is required. line 86 , connect.php controller';
        }
    }
like image 832
Jeromie Devera Avatar asked Dec 17 '25 11:12

Jeromie Devera


1 Answers

You should check the received Access Token before you continue with your application logic. How to do this is described here:

https://developers.facebook.com/docs/facebook-login/permissions/v2.0#checking

If you then recognize that the User didn't give the email permission, you can resend him to the permission dialog as described here:

https://developers.facebook.com/docs/facebook-login/permissions/v2.0#handling

Quote:

If someone has declined a permission for your app, the login dialog won't let your app re-request the permission unless you pass auth_type=rerequest along with your request.

like image 84
Tobi Avatar answered Dec 19 '25 07:12

Tobi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!