Laravel Facebook-Login / missing email

不羁的心 提交于 2019-12-06 11:42:27

There are instances that facebook will not return an email. This can be because the user has not set a primary email, or their email has not been validated. In this case, your logic should check to see if an email was returned, if not, use their facebook email. FacebookUsername@facebook.com

//I used with sentry

        // 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 );
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!