这是我 100% 工作的代码:
class Auth extends MX_Controller {
public function __construct()
{
parent::__construct();
$this->template->set_layout('login');
}
//social login
public function social($provider)
{
try{
$this->load->library('HybridAuthLib');
$this->load->model('auth_model');
$serviceEnabled = $this->hybridauthlib->serviceEnabled($provider);
if ($serviceEnabled)
{
$this->service = $this->hybridauthlib->authenticate($provider);
if ($this->service->isUserConnected())
{
$user_profile = $this->service->getUserProfile();
if($this->auth_model->count_user_by_uid($user_profile->identifier) === 0)
{
$this->session->set_flashdata('message','You Dont have account.. Create one.');
redirect('/users/register','refresh');
}
else
{
$dump_data = $this->auth_model->get_by(array('provider_uid'=>$user_profile->identifier));
$user = $this->ion_auth->user($dump_data->user_id)->row();
$session_data = array(
'identity' => $user->{$this->config->item('identity', 'ion_auth')},
'username' => $user->username,
'email' => $user->email,
'user_id' => $user->id, //everyone likes to overwrite id so we'll use user_id
'old_last_login' => $user->last_login
);
$this->ion_auth->update_last_login($user->id);
$this->ion_auth->clear_login_attempts($this->config->item('identity', 'ion_auth'));
$this->session->set_userdata($session_data);
if ($this->config->item('remember_users', 'ion_auth'))
{
$this->ion_auth->remember_user($user->id);
}
$this->ion_auth->trigger_events(array('post_login', 'post_login_successful'));
$this->ion_auth->set_message('login_successful');
redirect('/','refresh');
}
}
else // Cannot authenticate user
{
$this->session->set_flashdata('message','Cannot authenticate user');
redirect('/users/auth/login/','refresh');
}
}
else // This service is not enabled.
{
$this->session->set_flashdata('message','This providers is not enabled.');
redirect('/users/auth/login/','refresh');
}
}
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))
{
$service->logout();
}
$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($this->service))
{
$this->service->logout();
}
log_message('error', 'controllers.HAuth.login: '.$error);
$this->session->set_flashdata('message', $error);
redirect('/users/auth/login/', 'refresh');
}
}
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 ADDONPATH.'/users/third_party/hybridauth/index.php'; //ADDONPATH is my modules path
}
}
我希望你能发现它有用。我将 ion_auth 用于主登录系统。auth_model 是一个小模型,用于检查用户是否启用了此提供者的名称,因为我希望用户即使使用另一个社交网络登录也拥有相同的数据。