benedmunds / CodeIgniter-Ion-Auth

Simple and Lightweight Auth System for CodeIgniter
http://benedmunds.com/ion_auth/
MIT License
2.34k stars 1.14k forks source link

How can I integrate Ion Auth as a third party? #1055

Closed Luc45 closed 7 years ago

Luc45 commented 7 years ago

I'm new to CodeIgniter, and trying to load Ion Auth as a third party.

Here's what I did:

application/controllers/Admin.php

class Admin extends CI_Controller {
    public function __construct() {
        $this->load->library('ion_auth');
        // Results in: Unable to load the requested class: Ion_auth
    }
}

After googling, I made a library to require third_party/ion_auth/libraries/ion_auth.php:

application/libraries/Ion_Auth_Loader.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ion_Auth_Loader {
    public function __construct() {
        require_once APPPATH.'third_party/ion_auth/libraries/Ion_auth.php';
    }
}

Now I can load Ion Auth on my controller, but I can't use it's models:

class Admin extends CI_Controller {
    public function __construct() {
        $this->load->library('ion_auth_loader');
    if (!$this->ion_auth_loader->logged_in()) {
        $this->template->load_admin_login();
        redirect('/admin/login');
    }
        // Results in: Call to undefined method Ion_Auth_Loader::logged_in()

        // If I replace ion_auth_loader->logged_in() with ion_auth->logged_in(), it shows:
        // Undefined property: Admin::$ion_auth
        // Probably because ion_auth has never been defined?
    }
}

How can I make Ion Auth work as a third_party?

lloricode commented 7 years ago

just copy a file from same folders, config, libraries, and models

dont put it on third party folder

you may just read Installation in readme in this repo

Luc45 commented 7 years ago

I don't think this was answered properly...

benedmunds commented 7 years ago

Sorry about that.

Should be able to accomplish it with:

$this->load->add_package_path(APPPATH.'third_party/ion_auth/');
$this->load->library('ion_auth');
benedmunds commented 7 years ago

Please reopen if this doesnt resolve your issue.

spitfire64 commented 7 years ago

I placed everything in application/third_party/ion_auth. Then, all I did was moving the Auth controller from application/third_party/ion_auth/controller to application/controllers. Then in that controller I added this row before the library load:

$this->load->add_package_path(APPPATH.'third_party/ion_auth/');

It works, the request localhost/my_site/auth renders the login page back to my browser.

Ben, is this the correct way to do it?

benedmunds commented 7 years ago

Yep that's it 👍

muhammedsafeero commented 5 years ago

Can't load the ion_auth library by using $autoload['libraries'] = array('ion_auth');, when Installing Ion Auth by using third_party structure.

It will cause `A PHP Error was encountered Severity: Notice

Message: Undefined property: Controller_name::$db

Filename: models/Ion_auth_model.php

Line Number: 212`

So should use $this->load->library('ion_auth'); to load library on both $autoload['packages'] = array(APPPATH.'third_party/ion_auth'); (in application/config/autoload.php) and $this->load->add_package_path(APPPATH.'third_party/ion_auth/'); (in controller) ways.

If anything wrong correct me, this solved my problem that noticed before (` Severity: Notice

Message: Undefined property: Controller_name::$db

Filename: models/Ion_auth_model.php

Line Number: 212`)

Thank you

benedmunds commented 5 years ago

@muhammedsafeer thanks for sharing. Would you mind submitting a PR with a documentation update on how to do this.

muhammedsafeero commented 5 years ago

Your welcome @benedmunds, and I will try to submit PR but only after confirm that it is a error free method that I follow.

hasan-almujtaba commented 5 years ago

Sorry about that.

Should be able to accomplish it with:

$this->load->add_package_path(APPPATH.'third_party/ion_auth/');
$this->load->library('ion_auth');

what should i do if i want to load this library on autoload? i have tried this on autoload.php

$autoload['packages'] = array(APPPATH.'third_party/ion-Auth');

benedmunds commented 5 years ago

@Hasan-Almujtaba autoloading third party packages doesn’t seem to work in CodeIgniter. I’ve seen complaints about this for awhile now. One workaround that people use is to create another class, auto load that, and it’s only job is to loading in the third part class.