Search code examples
laravelfacebook-loginlaravel-socialite

How do I check if the user chose not to grant my app access after logging in with social media?


I'm following the Socialite github documentation, and the controller method below works just fine if the user successfully logs in with Facebook and grants my app access.

public function handleProviderCallback()
{
    $user = Socialite::driver('facebook')->user();
    dd($user);

    // $user->token;
}

However, how do I know if a user selected "Cancel" when Facebook asks for the user's permissions?

Currently, I receive the error below after I click "Cancel" on the Facebook page that asks for my permission to share my info with the app:

ClientException in RequestException.php line 111: 
Client error: `POST https://graph.facebook.com/oauth/access_token` resulted in a `400 Bad Request` response:
{"error":{"message":"Missing authorization code","type":"OAuthException","code":1,"fbtrace_id":"H75M4eMC+1f"}}

Solution

  • I updated my code to the following, which did the trick:

    public function handleProviderCallbackFacebook(Request $request)
    {
    if ($request->has('code')) {
      $user = Socialite::driver('facebook')->user();
      dd($user);
    }
    else 
    {
    echo "fail";
    }
    }