Facebook登录不在Laravel中

时间:2018-01-23 13:22:34

标签: facebook laravel facebook-graph-api laravel-5 laravel-4

我想使用以下代码在Laravel中使用facebook创建登录模块,但问题是会话变量是空白的,因此它不会重定向到主页。点击facebook的登录按钮后,我的程序控制移动到以下其他部分return Response::make('Not loggedin', 400);,然后我看到

jquery.min.js:4在我的浏览器控制台中获取http://localhost/Script/login/fb 400(错误请求)。

我是Laravel的新手。任何帮助/建议将受到高度赞赏。

  

UserController.php

public function loginWithFb() {


$redirectUrl = Input::get('redirect_url');
$facebookBaseConfig = Config::get('facebook');
$config = app('siteConfig');
$facebookConfig = $config['main']['social']['facebook'];
$facebookConfig['appId'] = empty($facebookConfig['appId']) ? '' : $facebookConfig['appId'];
$facebookConfig['secret'] = empty($facebookConfig['secret']) ? '' : $facebookConfig['secret'];
FacebookSession::setDefaultApplication($facebookConfig['appId'], $facebookConfig['secret']);
$helper = new FacebookJavaScriptLoginHelper();
$session = null;

function getUserDataFromFb($session){
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request->execute();
    $graphObject = $response->getGraphObject()->asArray();
    return $graphObject;
}

try {
    $session = $helper->getSession();
} catch(FacebookRequestException $ex) {
    // When Facebook returns an error
} catch(\Exception $ex) {
    // When validation fails or other local issues
}


if(Request::ajax()) {

    if ($session) {
      // Logged in.
        $uid = $session->getUserId();
        $accessToken = $session->getToken();
        $profile = Profile::whereUid($uid)->first();
        if (empty($profile)) {
            $me = getUserDataFromFb($session);  
            $user = new User;
            $user->name = $me['first_name'].' '.$me['last_name'];
            $user->email = $me['email'];
            $user->photo = 'https://graph.facebook.com/'.$uid.'/picture?type=large';

            $user->save();

            $profile = new Profile();
            $profile->uid = $uid;
            //$profile->username = $me['username']; //Username not available in the new Facebook API
            $profile->access_token = $accessToken;
            $profile = $user->profiles()->save($profile);

        }
        else {
            $profile->access_token = $accessToken;
            $profile->save();
        }
        $user = $profile->user;
        Auth::login($user);
        return Response::json(array('user' => $user));
    } else {
        return Response::make('Not loggedin', 400);
    }
} else{
    if ($session) {
        if($redirectUrl) {
            return Redirect::to($redirectUrl);
        } else{
            return Redirect::route('home');
        }
    }
    return Redirect::route('login');
}

}

  

查看:

<script>
    (function(){
        window.loginWithFb = function(){
            FB.login(function(response) {
               if (response.authResponse) {
                 FB.api('/me', function(response) {
                    console.log('Logged in as ' + response.name + '.');
                 });
               } else {
                 $('#loginError').removeClass('hide').html('<div class="alert alert-error">' + __('fbPermissionError') + '</div>');
               }
            }, {scope: 'email'});
        }

        var body = $('body');
        body.on('click', '[data-action="loginWithFB"]', function(e){
            loginWithFb();
            e.preventDefault();
        });
        body.on('loggedIn', function(){
            var loginModal = $('#loginModal');
            loginModal.modal('hide');
        });
        body.on('loggedIn:fb', function(){
            if(!User.isLoggedIn()) {
                var loginModal = $('#loginModal');
                loginModal.addClass("logging-in");
                $.get(BASE_PATH + '/login/fb').success(function(response){
                    User.setData(response.user);
                }).fail(function(response){
                    $('body').trigger('login:error', response);
                }).always(function(){
                    loginModal.removeClass("logging-in");
                });
            }
        });
        body.on('prompt-login', function(e, message){
            var loginModal = $('#loginModal');
            loginModal.find('.login-prompt-message').html(message);
            loginModal.modal('show');
        });
    })();

</script>

0 个答案:

没有答案