CodeIgniter-Ion-Auth icon indicating copy to clipboard operation
CodeIgniter-Ion-Auth copied to clipboard

Extending IonAuth from the third party folder on Codeigniter4

Open internetics opened this issue 2 years ago • 7 comments

I have IonAuth running in the ThirdParty folder on Codeigniter4. I have extended the controller, in that I have Auth.php running in my app/Controllers directory and that is working.

How can I extend the IonAuth library, the model and the language files etc? These files get ignored, unlike the Auth.php controller. I don't want to edit the files in the ThirdParty folder, to make it easier to upgrade etc. Thanks.

internetics avatar Jan 16 '22 06:01 internetics

You should be able to create a new library that extends the one in ThirdParty, then include that in your controller instead.

benedmunds avatar Jan 19 '22 19:01 benedmunds

class Tzilacatzin extends \CodeIgniter\Controller { public $IonAuth = null;

public function __construct(){
    $this->IonAuth =  service('ionauth');
}

class Services extends BaseService { public static function ionauth(IonAuth $config = null,$getShared = true) { if ($getShared){ return static::getSharedInstance('ionauth',$config); } return new \App\ThirdParty\IonAuth\IonAuth(); } }

[image: image.png]

YOU CAN CREATE A SERVICE

El mié, 19 ene 2022 a la(s) 13:34, Ben Edmunds @.***) escribió:

You should be able to create a new library that extends the one in ThirdParty, then include that in your controller instead.

— Reply to this email directly, view it on GitHub https://github.com/benedmunds/CodeIgniter-Ion-Auth/issues/1546#issuecomment-1016801233, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACBZA7HIYOWZRPFCEX4Y5MLUW4G25ANCNFSM5MCBRMHQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

junglaCODE avatar Jan 19 '22 20:01 junglaCODE

Thanks @benedmunds and @junglaCODE, but that doesn't really get to the bottom of it. I am on v4 ionAuth ansd v4 Codeigniter. I can get the controller to work (and sit in the app/controllers folder) with the code below. But I cannot seem do they same for the library, model, language files etc.

What am I missing?

<?php namespace App\Controllers;



class Auth extends \IonAuth\Controllers\Auth
{

  
protected $viewsFolder = 'Views\auth';



public function index()
{
	if (! $this->ionAuth->loggedIn())
	{
		// if not logged in then redirect them to the login page
		return redirect()->to('/auth/login');
	}

etc

internetics avatar Jan 23 '22 05:01 internetics

Can you zip up an example project with what you’ve tried. You can email it to me at ben.edmunds at gmail

benedmunds avatar Jan 23 '22 13:01 benedmunds

Hi, follow the steps below carefully. Step 1: Download this. Extract zip file in CI4\app\ThirdParty.

Step 2: Go to path CI4\app\Config. Edit file Autoload.php as follows:

    public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        'Config'      => APPPATH . 'Config',
        //Add IonAuth to CI4 Project
        'IonAuth' 	  => APPPATH . 'ThirdParty\CodeIgniter-Ion-Auth-4.0.3',
        
    ];

Step 3: Importe the tables manually(by phpmyadmin) in the database or use the following command: php spark migrate --all then php spark db:seed "IonAuth\Database\Seeds\IonAuthSeeder"

Step 4: Go to path CI4\app\Controllers and create a controller called Auth.php with the following content:

<?php
namespace APP\Controllers;
/**
 * Class Auth
 *
 * @property Ion_auth|Ion_auth_model $ion_auth      The ION Auth spark
 * @package  CodeIgniter-Ion-Auth
 * @author   Ben Edmunds <[email protected]>
 * @author   Benoit VRIGNAUD <[email protected]>
 * @license  https://opensource.org/licenses/MIT	MIT License
 */
class Auth extends \IonAuth\Controllers\Auth
{

	/**
	 * Validation list template.
	 *
	 * @var string
	 * @see https://bcit-ci.github.io/CodeIgniter4/libraries/validation.html#configuration
	 */
	protected $validationListTemplate = 'list';

	/**
	 * Views folder
	 * Set it to 'auth' if your views files are in the standard application/Views/auth
	 *
	 * @var string
	 */
	protected $viewsFolder = 'IonAuth\Views\auth';

}

NOTE: By doing this(Step 4) you can specify the view folder and validationListTemplate for be customized.Just specify the path. For example change protected $ viewsFolder = 'IonAuth\Views\auth'; To protected $ viewsFolder = 'App\Views\auth'; Step 5: The last step is how to use it in your controllers.Go to path CI4\app\Controllers and create a controller called Yorcontroller.php with the following content:

<?php
namespace App\Controllers;

class Yorcontroller extends App\Controllers\Auth
{
    	/**
	 * Construct
	 */
    public function __construct()
    {
	$this->ionAuth    = new \IonAuth\Libraries\IonAuth();
   }
    public function index()
    {
     
                $username='+989118847648';
                $password='passwordddd';
                $email='[email protected]';
                $additional_data = array(
                                'first_name' => 'Pooya',
                                'last_name' => 'parsa',
                                );
                // Sets user to member.
                $group = array('2'); 
               // register new user 
                $this->ionAuth->register($username, $password, $email, $additional_data, $group);
    }
}

entering address yoursite.com/Yorcontroller A new profile user is now created. Other library methods are available in the same way.example :

                if( $this->ionAuth->loggedIn()){
                    echo 'login OK';
                }else{
                        echo 'login NOT OK';
                }

@internetics Enjoy Enjoy and Enjoy.

datamweb avatar Jan 27 '22 12:01 datamweb

Thanks @datamweb . But as you can see in my code above yours I already have extended the Auth controller, and I have specified where the views are with:

protected $viewsFolder = 'Views\auth';

BUT what I really need to know is how to extend the languages folder and files, specify a new model, library etc etc - how can I get ionAuth to see my new files? NOT just the controller and view files.

@benedmunds I won't be able to send on my files, but I am hoping this request is simple enough!

Thanks for your help all!

internetics avatar Feb 01 '22 23:02 internetics

Thanks @datamweb . But as you can see in my code above yours I already have extended the Auth controller, and I have specified where the views are with:

protected $viewsFolder = 'Views\auth';

BUT what I really need to know is how to extend the languages folder and files, specify a new model, library etc etc - how can I get ionAuth to see my new files? NOT just the controller and view files.

@benedmunds I won't be able to send on my files, but I am hoping this request is simple enough!

Thanks for your help all!

I do not know if I understood what you meant correctly or not, because English is not good. To extend from the Config file. You must copy file app\ThirdParty\CodeIgniter-Ion-Auth-4.0.3\Config\IonAuth.php to path app\Config\IonAuth.php . Then apply the following changes:

<?php
namespace Config;

class IonAuth extends \IonAuth\Config\IonAuth
{
....
//yourfunction
}

To extend from your model file, you need to copy file app\ThirdParty\CodeIgniter-Ion-Auth-4.0.3\Models\IonAuthModel.php and put it in path app\Models\IonAuthModel.php .Then apply the following changes:

<?php
namespace App\Models;
class IonAuthModel extends \IonAuth\Models\IonAuthModel
{
......
//yourfunction
}

And ... Regarding the language file, you can delete or rename folder app\ThirdParty\CodeIgniter-Ion-Auth-4.0.3\Language (for example :: app\ThirdParty\CodeIgniter-Ion-Auth-4.0.3\UnuseLanguage). then move the two files(Auth.php and IonAuth.php) to path app\Language\en and apply the desired changes. Did you mean that?

datamweb avatar Feb 02 '22 13:02 datamweb