title: google-oauth library working in session in MVC PHP
body: <p>Hi I would like to learn something from here, basically I want the access of my system to be in google account somehow I manage to do the following</p>
  
  <ul>
  <li>get client id, redirect uris, secret keys</li>
  <li>authentication from google account</li>
  <li>get token</li>
  </ul>
  
  <p>but I i felt I was doing wrong all in some part, this is the <code>Oauth2callback</code> </p>
  
  <pre><code>class Oauth2callback extends CI_Controller {
  
  
      function __construct(){
          parent::__construct();
  
          $this-&gt;load-&gt;helper('url');
          $this-&gt;load-&gt;library('session');
          require_once APPPATH.'libraries/Google/Client.php';
          session_start();
  
  
      }
  
      public function index()
      {
  
  
          $client_id = $this-&gt;config-&gt;item('client_id');
          $client_secret = $this-&gt;config-&gt;item('client_secret');
          $redirect_uri = $this-&gt;config-&gt;item('redirect_uri');
  
          $client = new Google_Client();
          $client-&gt;setClientId($client_id);
          $client-&gt;setClientSecret($client_secret);
          $client-&gt;setRedirectUri($redirect_uri);
          $client-&gt;addScope("https://www.googleapis.com/auth/userinfo.email");
          $client-&gt;addScope("https://www.googleapis.com/auth/userinfo.profile");
  
          if (isset($_GET['code'])) {
              $client-&gt;authenticate($_GET['code']);
              $_SESSION['access_token'] = $client-&gt;getAccessToken();
              $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  
              header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
          }
  
          if (isset($_SESSION['access_token']) &amp;&amp; $_SESSION['access_token']) {
              $client-&gt;setAccessToken($_SESSION['access_token']);
          } else {
              $authUrl = $client-&gt;createAuthUrl();
          }
  
          if ($client-&gt;getAccessToken()) {
              $_SESSION['access_token'] = $client-&gt;getAccessToken();
  
  
          }
  
  
          if(isset($authUrl)) {
              header('location:'. base_url());
          }else{
              header('location:'. base_url().'dashboard');
          }
      }
  </code></pre>
  
  <p>but this is my index controller the <code>Login</code> it only has button <code>sign in with Google</code>
  
  
  <pre><code>class Login extends CI_Controller {
  
  
      function __construct(){
          parent::__construct();
  
          $this-&gt;load-&gt;helper('url');
          $this-&gt;load-&gt;library('session');
          require_once APPPATH.'libraries/Google/Client.php';
          session_start();
      }
  
      public function index()
      {
          //$this-&gt;checkSession();
          $client_id = $this-&gt;config-&gt;item('client_id');
          $client_secret = $this-&gt;config-&gt;item('client_secret');
          $redirect_uri = $this-&gt;config-&gt;item('redirect_uri');
  
          $client = new Google_Client();
          $client-&gt;setClientId($client_id);
          $client-&gt;setClientSecret($client_secret);
          $client-&gt;setRedirectUri($redirect_uri);
          $client-&gt;addScope("https://www.googleapis.com/auth/userinfo.email");
          $client-&gt;addScope("https://www.googleapis.com/auth/userinfo.profile");
  
              $authUrl = $this-&gt;data['authUrl'] = $client-&gt;createAuthUrl();
              $this-&gt;load-&gt;view('login/index.php',$this-&gt;data);
              $this-&gt;load-&gt;view('template/pre_footer');
              $this-&gt;load-&gt;view('template/footer');
  
  
      }
  }
  </code></pre>
  
  <p><br></p>
  
  <p>what would be the right process of this using MVC PHP I need to do the ff. :<br>
  A. click button <code>sign in to google</code><br>
  B. get token and save it to session<br>
  C. used the token to my entire system (in every controller)
  <br><br>
  what I have right now is A &amp; B but the C i totally don't know what to do.</p>
  
  <p>could anyone help me with this. any suggestion comment is well appreciated. thanks in advance.</p>