How to avoid Podio reauthentification each and every time I query data from Podio?

≯℡__Kan透↙ 提交于 2019-12-12 03:52:44

问题


I'm writing a codeigniter controller. Users log in to the website as Podio users. After logging in, they are redirected to dashboard.

Function test_auth makes the authentification and redirects users to dashboard.

The problem is : in dashboard function I have to reauthentificate if I want to call, for example, function PodioOrganization::get_all( );

Here is my code:

class User extends CI_Controller {

    /**
     * __construct function.
     * 
     * @access public
     * @return void
 */
public function __construct() {

    parent::__construct();
    $this->load->library(array('session'));
    $this->load->helper(array('url'));
    $this->load->model('user_model');       
}

public function dashboard()
{       
    if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != true) {//not logged         
        redirect(base_url().'user/login');
    }else
    {

        $data=(array)$this->user_model->get_user($_SESSION['user_id']);

        //$data=(array)$this->user_model->get_user(3);
        $data['title'] ='Dashboard Page';
            //'heading' => 'My Heading',
            //'message' => 'My Message'

        Podio::setup(CLIENT_ID, CLIENT_SECRET);

        $orgs=PodioOrganization::get_all(  );

            $this->load->view('header',$data);
            $this->load->view('user/page_header',$data);
            $this->load->view('user/dashboard',$data);
            $this->load->view('user/footer');                                           
    }               
}       

public function test_auth()
{
    // Set up the REDIRECT_URI -- which is just the URL for this file.

    if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true ) {
        redirect(base_url().'user/dashboard');
    }
    else{

        Podio::setup(CLIENT_ID, CLIENT_SECRET);

        if (!isset($_GET['code']) && !Podio::is_authenticated()) {

          // User is not being reidrected and does not have an active session
          // We just display a link to the authentication page on podio.com
          $auth_url = htmlentities(Podio::authorize_url(REDIRECT_URI));
          //print "<a href='{$auth_url}'>Start authenticating</a>";
          redirect($auth_url); 

        } elseif (Podio::is_authenticated()) {

          // User already has an active session. You can make API calls here:
          print "You were already authenticated and no authentication is needed.";

        }
        elseif (isset($_GET['code'])) {

          // User is being redirected back from podio.com after authenticating.
          // The authorization code is available in $_GET['code']
          // We use it to finalize the authentication

          // If there was a problem $_GET['error'] is set:
          if (isset($_GET['error'])) {
            print "There was a problem. The server said: {$_GET['error_description']}";
          }
          else {
            // Finalize authentication. Note that we must pass the REDIRECT_URI again.
            Podio::authenticate_with_authorization_code($_GET['code'], REDIRECT_URI);


                $_SESSION['user_id']      = 1;
                $_SESSION['logged_in']    = (bool)true;
                $_SESSION['access_token'] = (string)Podio::$oauth->access_token ;
                $_SESSION['refresh_token']= (string)Podio::$oauth->refresh_token  ;

                redirect(base_url().'user/dashboard');                                                  
          }

        }
    }   
}   

And I get a PodioAuthorizationError , for calling $orgs=PodioOrganization::get_all( ); without authentificating first (although I have done this, prior, in test_auth function )

And once I have called Podio::setup(CLIENT_ID, CLIENT_SECRET); in test_auth() why do I have to call it again in dashboard() . The $client_id and $client_secret insite setup() function should be set. Why does Podio::setup looses its values?

$client_id and $client_secret are declared static inside class Podio, so they should keep their values but they don't. Why?

来源:https://stackoverflow.com/questions/45204272/how-to-avoid-podio-reauthentification-each-and-every-time-i-query-data-from-podi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!