Laravel Facebook-Login / Missing Email

I am trying to create Facebook-Login in my Laravel app. However, the array that I get with all the user information does not contain user email, although I already ask for permission to receive email.

This is my code:

Route::get('login/fb', function() { $facebook = new Facebook(Config::get('facebook')); $params = array( 'redirect_uri' => url('/login/fb/callback'), 'scope' => 'email', ); return Redirect::to($facebook->getLoginUrl($params)); }); Route::get('login/fb/callback', function() { $code = Input::get('code'); if (strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with Facebook'); $facebook = new Facebook(Config::get('facebook')); $me = $facebook->api('/me'); return $me; 

The return of $ me gives me all the important user information except the email address.

Is there any way to fix this?

Any help would be greatly appreciated.

Thanks.

+2
source share
2 answers

There are times when facebook does not return an email. This may be due to the fact that the user did not set a primary email address or their email was not verified. In this case, your logic should check if the email has been returned; if not, use facebook email. FacebookUsername@facebook.com

+1
source

// I used with the sentinel

  // get data from input $code = Input::get( 'code' ); // get fb service $fb = OAuth::consumer( 'Facebook' ); // check if code is valid // if code is provided get user data and sign in if ( !empty( $code ) ) { // This was a callback request from facebook, get the token $token = $fb->requestAccessToken( $code ); // Send a request with it $result = json_decode($fb->request( '/me?fields=id,name,first_name,last_name,email,photos' ), true); $message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name']. $result['email']; //echo $message. "<br/>"; //Var_dump //display whole array(). //echo('http://graph.facebook.com/'.$result['id'].'/picture?type=large<br>'); //dd($result); $user = \User::where("email",$result['email'])->first(); if($user!=NULL){ $userxx = Sentry::findUserByLogin($result['email']); Sentry::login($userxx, false); return Redirect::to('Beşiktaş'); } else { $k=str_random(8); $user = Sentry::register(array( 'activated' => 1, 'facebook' => 1, 'password' => $k, 'email' => $result['email'], 'first_name' =>$result['first_name'], 'last_name' => $result['last_name'] , 'avatar' => 'http://graph.facebook.com/'.$result['id'].'/picture?type=large', )); Sentry::login($user, false); return Redirect::to('Beşiktaş'); } } // if not ask for permission first else { // get fb authorization $url = $fb->getAuthorizationUri(); // return to facebook login url return Redirect::to( (string)$url ); } 
+1
source

Source: https://habr.com/ru/post/1483160/


All Articles