我正在使用codeIgniter构建一个管理面板,我希望Google登录用于登录。我已经尝试过在Codexworld.com中显示的技术,但在重定向后显示未找到的页面,即使控制器和功能都在那里。从现在开始,真的需要帮助一直坚持这个。
我不明白代码出错的地方。
我的控制器
public function authGoogle()
{
$this->load->helper('url');
$this->load->library('google');
//google login url
$data['loginURL'] = $this->google->loginURL();
//load google login view
$this->load->view('login',$data);
}
public function profile(){
//redirect to login page if user not logged in
if(!$this->session->userdata('loggedIn')){
// redirect('/user_authentication/');
}
//get user info from session
// $data['userData'] = $this->session->userdata('userData');
//load user profile view
// $this->load->view('user_authentication/profile',$data);
}
public function auth_user()
{
// echo "This is working";
$this->load->helper('url');
if($this->session->userdata('loggedIn') == true)
{
echo 'this is working';
}
if(isset($_GET['code']))
{
$this->load->library('google');
//authenticate user
$this->google->getAuthenticate();
//get user info from google
$gpInfo = $this->google->getUserInfo();
//preparing data for database insertion
$userData['oauth_provider'] = 'google';
$userData['oauth_uid'] = $gpInfo['id'];
$userData['first_name'] = $gpInfo['given_name'];
$userData['last_name'] = $gpInfo['family_name'];
$userData['email'] = $gpInfo['email'];
$userData['gender'] = !empty($gpInfo['gender'])?$gpInfo['gender']:'';
$userData['locale'] = !empty($gpInfo['locale'])?$gpInfo['locale']:'';
$userData['profile_url'] = !empty($gpInfo['link'])?$gpInfo['link']:'';
$userData['picture_url'] = !empty($gpInfo['picture'])?$gpInfo['picture']:'';
//insert or update user data to the database
$this->load->model('Usermodel');
$userID = $this->Usermodel->checkUser($userData);
//store status & user info in session
$this->session->set_userdata('loggedIn', true);
$this->session->set_userdata('userData', $userData);
//redirect to profile page
echo "This is working";
}
}
public function logout(){
$this->load->helper('url');
//delete login status & user info from session
$this->session->unset_userdata('loggedIn');
$this->session->unset_userdata('userData');
$this->session->sess_destroy();
//redirect to login page
// redirect('/Welcome/authGoogle');
}
我的模态
public function checkUser($data = array()){
$this->tableName = 'users';
$this->primaryKey = 'id';
$this->db->select($this->primaryKey);
$this->db->from($this->tableName);
$this->db->where(array('oauth_provider'=>$data['oauth_provider'],'oauth_uid'=>$data['oauth_uid']));
$query = $this->db->get();
$check = $query->num_rows();
if($check > 0){
$result = $query->row_array();
$data['modified'] = date("Y-m-d H:i:s");
$update = $this->db->update($this->tableName,$data,array('id'=>$result['id']));
$userID = $result['id'];
}else{
$data['created'] = date("Y-m-d H:i:s");
$data['modified']= date("Y-m-d H:i:s");
$insert = $this->db->insert($this->tableName,$data);
$userID = $this->db->insert_id();
}
return $userID?$userID:false;
}
我的Google配置
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| Google API Configuration
| -------------------------------------------------------------------
| client_id string Your Google API Client ID.
| client_secret string Your Google API Client secret.
| redirect_uri string URL to redirect back to after login.
| application_name string Your Google application name.
| api_key string Developer key.
| scopes string Specify scopes
*/
$config['google']['client_id'] = 'XXXX';
$config['google']['client_secret'] = 'XXX';
$config['google']['redirect_uri'] = 'XXX';
$config['google']['application_name'] = 'XXX';
$config['google']['api_key'] = '';
$config['google']['scopes'] = array();
?>
我的谷歌oauth图书馆
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Google{
public function __construct(){
$CI =& get_instance();
$CI->config->load('google');
require_once APPPATH .'libraries/gplus/vendor/google/apiclient/src/Google/autoload.php';
require APPPATH .'libraries/gplus/vendor/google/apiclient/src/Google/Client.php';
require APPPATH .'libraries/gplus/vendor/google/apiclient/src/Google/Service/Oauth2.php';
$this->client = new Google_Client();
$this->client->setApplicationName($CI->config->item('application_name', 'google'));
$this->client->setClientId($CI->config->item('client_id', 'google'));
$this->client->setClientSecret($CI->config->item('client_secret', 'google'));
$this->client->setRedirectUri($CI->config->item('redirect_uri', 'google'));
$this->client->setDeveloperKey($CI->config->item('api_key', 'google'));
$this->client->setScopes($CI->config->item('scopes', 'google'));
$this->client->setAccessType('online');
$this->client->setApprovalPrompt('auto');
$this->client->setScopes('email');
$this->oauth2 = new Google_Service_Oauth2($this->client);
}
public function loginURL() {
return $this->client->createAuthUrl();
}
public function getAuthenticate() {
return $this->client->authenticate();
}
public function getAccessToken() {
return $this->client->getAccessToken();
}
public function setAccessToken() {
return $this->client->setAccessToken();
}
public function revokeToken() {
return $this->client->revokeToken();
}
public function getUserInfo() {
return $this->oauth2->userinfo->get();
}
}
?>