Welcome Guest, Not a member yet? Register   Sign In
CI 4 simple session auth system problem
#1
Sad 
(This post was last modified: 10-30-2019, 09:08 PM by adelbak.)

Firstly I'm sorry for my very*100 bad Engilsh

Please help to solve this problem, and thank you in advance.
I am programming a site based on an simple session auth system, but I have had two problems Huh Huh .
- Let's take a look at the files:

BaseController.php Controller:
PHP Code:
<?php
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
BaseController extends Controller
{

    protected 
$helpers = [];

    protected $session;

    protected $validation;
    
    
/**
     * Constructor.
     */
    
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);
        
        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
           $this->validation =  \Config\Services::validation();
          $this->session = \Config\Services::session();

        $this->session->start();

    }
    
    protected function 
loggedIn()
    {    
        if($this->session->has('loggedIn'))
        {
            return redirect()->to(base_url().'/dashboard/index');
            
        }
    }
    
    public function 
notLoggedIn()
    {        
        if(!$this->session->has('loggedIn'))
        {
            return redirect()->to(base_url().'/auth/index');
            
        }
    }    


Auth.php Controller:
PHP Code:
<?php namespace App\Controllers;

use 
App\Models\AuthModel;

class 
Auth extends BaseController
{
    protected $authModel;

    public function __construct() {
    
           $this->authModel = new AuthModel();
        
$this->loggedIn();
    }
        
    
public function index()
    {

    
    $data = [
                'title'     => "Login Page"
        ];
        
        return view('pages/login'$data);
        

    } 

Problem 1: when i browse Auth (auth/index): Call to a member function has() on null Error...Even though I called and start session class/service from initController function in BaseController Huh .
- When the problem is solved in a ugly way by call  session class/service inside loggedIn() function like this:
PHP Code:
protected function loggedIn()
 { 
      $this->session = \Config\Services::session();
 
        if($this->session->has('loggedIn'))
        {
            return redirect()->to(base_url().'/dashboard/index');
 
 }
 } 


The second problem: the function does not redirect to /dashboard/index, even  i'm sure a having a successful login and loggedIn = username not null or empty or false Huh

Help..Thanks
Reply
#2

(This post was last modified: 10-31-2019, 12:40 PM by dave friend.)

Remove the following line from BaseController. It is not needed because the session library does this for you.

PHP Code:
$this->session->start(); 

That may be the reason $this->session is coming up null.


Try this change for loggedIn()

PHP Code:
protected function loggedIn()
{
    if ($this->session->has('loggedIn'))
    {
        return redirect()->to(base_url('dashboard/index'));
    }

    // then not logged in
    return redirect()->to(base_url('auth/index'));


Then you can remove public function notLoggedIn()
Reply




Theme © iAndrew 2016 - Forum software by © MyBB