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

Call to a member function loggedIn() on null #1421

Closed Marcolino92 closed 4 years ago

Marcolino92 commented 4 years ago

I managed to adapt it and make it work, I have to do some practice. Once logged in, I set it to be redirected to the index of another controller (not auth) but displays this error:

Call to a member function loggedIn() on null

if (!$this->ionAuth->loggedIn())
{
    echo 'Non sei collegato';
} 
benedmunds commented 4 years ago

Do you have Ion Auth loaded in that controller?

Marcolino92 commented 4 years ago

On codeigniter 3, I loaded model, library from autoload.php, on Codeigniter 4 how can I do?

My Home controller:

`<?php

namespace App\Controllers; use App\Models\TesttModel;

class Home extends BaseController{

public function index(){

    $model = new TestModel();

    $data = [
        'title' => 'Test',
        'testing' => $model->getTesting()

    ];

    echo view('themes/index', $data);
}

public function view($slug = null){

    $model = new testModel();
    $data['testing'] = $model->getTesting($slug);

}

//--------------------------------------------------------------------

}`

benedmunds commented 4 years ago

In your Config/Autoload.php, add 'IonAuth' => ROOTPATH . 'YOUR-ION_AUTH-FOLDER',

Marcolino92 commented 4 years ago

I moved all the content to the respective folders in the app, so there is no IonAuth folder in the root. Consequently what path do I enter here?

            $psr4 = [
        'App'         => APPPATH,                // To ensure filters, etc still found,
        APP_NAMESPACE => APPPATH,                // For custom namespace
        'Config'      => APPPATH . 'Config',
                    'IonAuth' => ROOTPATH . 'YOUR-ION_AUTH-FOLDER',
    ];
benedmunds commented 4 years ago

Well it depends on how you installed it. If you used composer then you can add the Use statement and it will autoload. See the full install instructions here:

https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/4/INSTALLING.md

Marcolino92 commented 4 years ago

I installed everything by uploading the files from the archive. I made the necessary adjustments by moving all the files to their respective folders in the app /.

The authentication system works, login, create user, logout etc. but I have another controller that manages my index (controllers / home.php). When I login I redirect to the other pages (outside of auth) but that error appears. So I have to import the library ... but it looks like I'm wrong.

Call to a member function logged_in() on null

benedmunds commented 4 years ago

See how we're loading the class in the constructor of the auth controller here: https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/4/Controllers/Auth.php#L73

Are you doing that in your other controller?

Marcolino92 commented 4 years ago

Yes, but nothing. This error always appears. This is my Home.php controller

namespace App\Controllers;
use App\Models\TestModel;

class Home extends BaseController {

    /**
     * Constructor
     *
     * @return void
     */
    public function __construct(){
        $this->ionAuth = new \App\Libraries\IonAuth();
        $this->validation = \Config\Services::validation();
        helper(['form', 'url']);
        $this->configIonAuth = config('IonAuth');
        $this->session = \Config\Services::session();
    }

    public function index(){

        $model = new TestModel();

        if(!$this->ionAuth->loggedIn()){
            // redirect them to the login page
            return redirect()->to('login');
        }

        $data = [
            'title' => 'Obituary',
            'test' => $model->getTest()

        ];

        echo view('themes/index', $data);
    }

    public function view($slug = null){

        $model = new TestModel();
        $data['test'] = $model->getTest($slug);

    }

    //--------------------------------------------------------------------

}
benedmunds commented 4 years ago

Try declaring the property before you assign it, similar to https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/dd61a429ed235f51d5e3eb9a0014cc96061b652b/Controllers/Auth.php#L34

benedmunds commented 4 years ago

Also, are you sure that IonAuth is under the \App namespace? Is that what you're using in the auth controller?

Marcolino92 commented 4 years ago

I noticed this, if I connect correctly on mydomain.com/test/login I am redirected correctly in the index. But if I log out and go to the index check the error. Can it be something related to the sessions?

bvrignaud commented 4 years ago

Why don't you use composer to load the library ?

Marcolino92 commented 4 years ago

Can I develop directly on hosting using composer or does it allow me to work only in virtual?

bvrignaud commented 4 years ago

Both with CD. And you don't have to keep libraries/framework code in git history. I use this script : https://gist.github.com/bvrignaud/176b7657c23a381b294021d3855f859c. But there many tools to do that.

Marcolino92 commented 4 years ago

I'm going crazy. To simplify everything, I avoided using other controllers for my index, using Controllers / Auth.php directly as the base and setting the "Auth" as the main in the routes and then being able to use the index as the basis of my site. Unfortunately it does not equally recognize the functions, such as checking if the user is logged in, or:

$user = $this->ionAuth->user()->row();
echo $user->email;
benedmunds commented 4 years ago

Closing due to inactivity. Please reopen if this is unresolved.