Create a gist now

Instantly share code, notes, and snippets.

@purdy /login.php Secret
Last active Dec 22, 2015

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