NVO
NVO

Reputation: 2703

Codeigniter RESTServer session

I'm building an API with CodeIgniter and the RestServer implementation from https://github.com/chriskacerguis/codeigniter-restserver

I want to use the 'session' authentication mode so I set this in config/rest.php:

$config['rest_auth'] = 'session';
$config['auth_source'] = 'userdata';

in config/config.php:

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

I have a controller Auth which have the following code:

class Auth extends My_Controller
{

    public function __Construct(){
        parent::__construct();

    }

    public function index(){


        $user = array(
            "id" => "1",
            "first_name" => "First",
            "insertion" => "",
            "last_name" => "Last"
        );

        $this->session->userdata = $user;

        pr($this->session->userdata);
    }
}

The session is stored in the database, and contains the data from $user. pr echos the data (pr is defined in hooks, and does print_r($data))

But.. when I run a controller witch extends the REST_Controller I always get an {"status":false,"error":"Unauthorized"} error.

I have searched a lot, but I can't find a solution for this (or nobody use the 'session' auth method?), but maybe someone here knows the solution?

Thanks!

EDIT: The controller for one of my API functions:

class Club extends REST_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Club_model');        
    }

    public function index_get(){
        if(!$this->get('id')){
            $clubs = $this->Club_model->get_all_clubs();
        }else{
            $clubs = $this->Club_model->get_club($this->get('id'));
        }

        if($clubs){
            $this->response($clubs, 200);
        }else{
            $this->response(null, 404);
        }
    }

EDIT 2: autoload.php I have added 'session' in config/autoload.php

$autoload['libraries'] = array('database','session');

Upvotes: 2

Views: 5370

Answers (2)

Pirooz Jenabi
Pirooz Jenabi

Reputation: 682

this is not error

you must write session variable in

$config['auth_source'] = '';

i write this code and solved problem (erplace above to down code in config/rest.php)

$config['auth_source'] = '__ci_last_regenerate';

Upvotes: 1

Test this.

$user = array(
        "id" => "1",
        "first_name" => "First",
        "insertion" => "",
        "last_name" => "Last"
    );

    $this->session->userdata = $user;

Then your config for example

$config['auth_source'] = 'last_name';

Upvotes: 1

Related Questions