Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending IonAuth from the third party folder on Codeigniter4 #1546

Open
internetics opened this issue Jan 16, 2022 · 8 comments
Open

Extending IonAuth from the third party folder on Codeigniter4 #1546

internetics opened this issue Jan 16, 2022 · 8 comments

Comments

@internetics
Copy link

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.

@benedmunds
Copy link
Owner

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

@junglaCODE
Copy link

junglaCODE commented Jan 19, 2022 via email

@internetics
Copy link
Author

internetics commented Jan 23, 2022

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

@benedmunds
Copy link
Owner

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

@datamweb
Copy link
Contributor

datamweb commented Jan 27, 2022

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 <ben.edmunds@gmail.com>
 * @author   Benoit VRIGNAUD <benoit.vrignaud@zaclys.net>
 * @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='Info@datamweb.ir';
                $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.

@internetics
Copy link
Author

internetics commented Feb 1, 2022

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!

@datamweb
Copy link
Contributor

datamweb commented Feb 2, 2022

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?

@jegadeshsundaram
Copy link

Hi Sir,

I am getting issue on APPPATH/Controllers/Auth.php at line 5

Error: Class "IonAuth\Controllers\Auth" not found

Screenshot 2024-09-26 at 18-15-04 Error

I have attached reference for your check. Please help me on this.

Thanks in Advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants