-
-
Save purdy/3a5da1001d2551ae6a52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Login extends CI_Controller { | |
public function relogin() { | |
$this->load->view( 'login' ); | |
} | |
public function not_approved() { | |
$this->load->view( 'blacklist' ); | |
} | |
public function logout() { | |
$this->session->sess_destroy(); | |
redirect( 'login/relogin' ); | |
} | |
public function revoke() { | |
$this->session->set_userdata( 'revoke', 1 ); | |
redirect( 'login' ); | |
} | |
public function index() { | |
require_once APPPATH . '/libraries/google-api-php-client/src/Google_Client.php'; | |
require_once APPPATH . '/libraries/google-api-php-client/src/contrib/Google_Oauth2Service.php'; | |
$client = new Google_Client(); | |
$client->setClientId( 'CLIENTID' ); | |
$client->setClientSecret( 'SECRET' ); | |
$client->setDeveloperKey( 'DEVKEY' ); | |
$client->setRedirectUri( site_url( 'login' ) ); | |
$client->setScopes( array( 'openid', 'email', 'profile' ) ); | |
$client->setApprovalPrompt( 'auto' ); | |
$oauth2 = new Google_Oauth2Service($client); | |
if ( $this->input->get( 'code' ) ) { | |
$client->authenticate(); | |
$this->session->set_userdata( array( 'token' => $client->getAccessToken() ) ); | |
redirect( 'login' ); | |
} | |
if ( $this->session->userdata( 'token' ) ) { | |
$client->setAccessToken( $this->session->userdata( 'token' ) ); | |
} | |
if ( $client->getAccessToken() ) { | |
if ( $this->session->userdata( 'revoke' ) ) { | |
$client->revokeToken(); | |
$this->session->sess_destroy(); | |
redirect( 'login/relogin' ); | |
} | |
else { | |
$token_details = json_decode( $client->getAccessToken() ); | |
$user = $oauth2->userinfo->get(); | |
$this->load->model( 'user_model', 'users' ); | |
$user_id = $this->users->setup_user( $user, $token_details ); | |
$this->session->set_userdata( array( | |
'user_id' => $user_id, | |
) ); | |
redirect( 'lead_approval' ); | |
} | |
} | |
else { | |
$auth_url = $client->createAuthUrl(); | |
redirect( $auth_url ); | |
} | |
} | |
} | |
/* End of login.php file */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment