Hybridigniter Auth library and social plugin

1.5k Views Asked by At

I have integrated the codeigniter hybrid auth library for social login. I have created the developer key for facebook , google and twitter. When i am trying to login with these api's, i got an error.I am running my application in localhost. Is it a problem while redirecting the url to localhost.

Facebook Authentication:

User has cancelled the authentication or the provider refused the connection.

Google Authentication:

Error: redirect_uri_mismatch

Twitter Authentication:

User has cancelled the authentication or the provider refused the connection.

Controller(hauth):

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class HAuth extends CI_Controller {

    public function index()
    {
        $this->load->view('hauth/home');
    }

    public function login($provider)
    {
        log_message('debug', "controllers.HAuth.login($provider) called");

        try
        {
            log_message('debug', 'controllers.HAuth.login: loading HybridAuthLib');
            $this->load->library('HybridAuthLib');

            if ($this->hybridauthlib->providerEnabled($provider))
            {
                log_message('debug', "controllers.HAuth.login: service $provider enabled, trying to authenticate.");
                $service = $this->hybridauthlib->authenticate($provider);

                if ($service->isUserConnected())
                {
                    
                    log_message('debug', 'controller.HAuth.login: user authenticated.');

                    $user_profile = $service->getUserProfile();

                    log_message('info', 'controllers.HAuth.login: user profile:'.PHP_EOL.print_r($user_profile, TRUE));

                    $data['user_profile'] = $user_profile;

                    $this->load->view('hauth/done',$data);
                }
                else // Cannot authenticate user
                {
                    show_error('Cannot authenticate user');
                }
            }
            else // This service is not enabled.
            {
                log_message('error', 'controllers.HAuth.login: This provider is not enabled ('.$provider.')');
                show_404($_SERVER['REQUEST_URI']);
            }
        }
        catch(Exception $e)
        {
            $error = 'Unexpected error';
            switch($e->getCode())
            {
                case 0 : $error = 'Unspecified error.'; break;
                case 1 : $error = 'Hybriauth configuration error.'; break;
                case 2 : $error = 'Provider not properly configured.'; break;
                case 3 : $error = 'Unknown or disabled provider.'; break;
                case 4 : $error = 'Missing provider application credentials.'; break;
                case 5 : log_message('debug', 'controllers.HAuth.login: Authentification failed. The user has canceled the authentication or the provider refused the connection.');
                         //redirect();
                         if (isset($service))
                         {
                            log_message('debug', 'controllers.HAuth.login: logging out from service.');
                            $service->logout();
                         }
                         show_error('User has cancelled the authentication or the provider refused the connection.');
                         break;
                case 6 : $error = 'User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again.';
                         break;
                case 7 : $error = 'User not connected to the provider.';
                         break;
            }

            if (isset($service))
            {
                $service->logout();
            }

            log_message('error', 'controllers.HAuth.login: '.$error);
            show_error('Error authenticating user.');
        }
    }

    public function endpoint()
    {

        log_message('debug', 'controllers.HAuth.endpoint called.');
        log_message('info', 'controllers.HAuth.endpoint: $_REQUEST: '.print_r($_REQUEST, TRUE));

        if ($_SERVER['REQUEST_METHOD'] === 'GET')
        {
            log_message('debug', 'controllers.HAuth.endpoint: the request method is GET, copying REQUEST array into GET array.');
            $_GET = $_REQUEST;
        }

        log_message('debug', 'controllers.HAuth.endpoint: loading the original HybridAuth endpoint script.');
        require_once APPPATH.'/third_party/hybridauth/index.php';

    }
}

/* End of file hauth.php */
/* Location: ./application/controllers/hauth.php */
2

There are 2 best solutions below

0
On

You may face problem running Facebook and other third party authentications on local host, and it may be because of different reasons. One possible solution you can try is set a virtual domain to access your CI website. http://www.kristengrote.com/blog/articles/how-to-set-up-virtual-hosts-using-wamp gives very clear instruction to set up virtual host on WAMP. and then do our Facebook app's settings accordingly.

You may have to check if relevant ports on your localhost are open, otherwise you may face problem in receiving back the authentication.

0
On

That Facebook error happened to me. My app had already been authorized but Facebook did not redirect properly after authorization. Go to Facebook>settings>apps and check whether your app is among the authorized ones. If so, delete it and try again.