mardi 23 janvier 2018

Facebook login not working in Laravel

I want to create login module using facebook in Laravel using the below code but the issue is the session variable is blank and so it is not redirecting to the home page. After clicking on the Login button of facebook my program control moves to the following else part return Response::make('Not loggedin', 400); and i see

jquery.min.js: 4 GET http://localhost/Script/login/fb 400 (Bad Request) in my browser console.

I am new to Laravel. Any help/suggestion will be highly appreciated.

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');
}

}

View:

<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>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire