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

357 views Asked by At

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"}}
2

There are 2 answers

0
Colin Hu On BEST ANSWER

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";
}
}
0
Sanzeeb Aryal On

Check for the request in callback function. If user choose not to grant access means no request sent to callback function:

public function handleProviderCallback(Request $request)
{
   if(!$request->all())
   {
      return redirect('login')->with('message','Social authentication cancelled'); 
   }

   $user = Socialite::driver('facebook')->user();
   dd($user);

}