laravel 6 login successfully but redirect to login page

Tags: , , , ,



enter image description here

My environment is MAMP server on MACOS.

LoginController.php

public function authenticate(Request $request)
    {
        $this->validate($request, [
            'email'   => 'required|email',
            'password' => 'required|min:8'
        ]);

        if (Auth::guard('web')->attempt(['email' => $request->email, 'password' => $request->password,'isactive'=>true], $request->get('remember'))) {

            try{
                $isAdmin = auth()->user()->isAdmin;

                $data = Permission::select('route','retailer_access','distributor_access')
                ->where('method','GET')
                ->get();
                $urlData = [];
                foreach($data as $url){
                    $myurl['route'] = $url['route'];
                    switch($isAdmin){
                        case 0:
                            $myurl['access'] = $url['retailer_access'];
                            break;
                        case 1:
                            $myurl['access'] = $url['distributor_access'];
                            break;
                        case 2:
                            $myurl['access'] = true;
                            break;
                    }
                    array_push($urlData,$myurl);
                }
                $is_admin = false;
                if($isAdmin == 2){
                    $is_admin=true;
                }

                $arr = ['urlData'=>$urlData,'is_admin'=>$is_admin];
                session($arr);
                return redirect()->intended('/home');
            }
            catch(Exception $e){
                return back()->with('failed','Invalid email or password.')
                    ->withInput($request->only('email'));
            }

        }
        return back()->with('failed','Invalid email or password.')
                    ->withInput($request->only('email'));
    }

HomeController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('permission');
    }

    /**
     * Show the application dashboard.
     *
     * @return IlluminateContractsSupportRenderable
     */
    public function index()
    {
        return view('home');
    }
}

The below array i set in session.

Array (
    [0] => Array ( [route] => setting/servicetype/add [access] => 1 )
    [1] => Array ( [route] => setting/servicetype [access] => 1 ) 
    [2] => Array ( [route] => setting/api/edit/{id} [access] => 1 )
    [3] => Array ( [route] => setting/api/add [access] => 1 )
    [4] => Array ( [route] => setting/api [access] => 1 )
    [5] => Array ( [route] => setting/carrier [access] => 1 )
    [6] => Array ( [route] => wallet/details/{id?} [access] => 1 )
    [7] => Array ( [route] => wallet [access] => 1 )
    [8] => Array ( [route] => permission [access] => 1 )
    [9] => Array ( [route] => home [access] => 1 )
    [10] => Array ( [route] => user/edit/{id} [access] => 1 )
    [11] => Array ( [route] => user/add [access] => 1 )
    [12] => Array ( [route] => user [access] => 1 )
    [13] => Array ( [route] => about [access] => 1 )
    [14] => Array ( [route] => logout [access] => 1 )
    [15] => Array ( [route] => password/confirm [access] => 1 )
    [16] => Array ( [route] => password/reset/{token} [access] => 1 )
    [17] => Array ( [route] => password/reset [access] => 1 )
    [18] => Array ( [route] => register [access] => 1 )
    [19] => Array ( [route] => login [access] => 1 )
    [20] => Array ( [route] => / [access] => 1 )
    [21] => Array ( [route] => api/user [access] => 1 )
    [22] => Array ( [route] => _ignition/styles/{style} [access] => 1 )
    [23] => Array ( [route] => _ignition/scripts/{script} [access] => 1 )
    [24] => Array ( [route] => _ignition/health-check [access] => 1 )
    [25] => Array ( [route] => settings/edit/{id} [access] => 1 )
)

Is there any limitation of session elements in my environment? When session element is less then 25, it’s working but when i add one more key value this issue is happening. Please help me to understand what is the problem here.

Answer

I Found the solution of this problem. I am using cookie session driver so I converted array $arr (in LoginController.php) to json_ecoded string.

$arrstr = json_encode($arr);

and use push() method to put in session.

$request->session->push('permission',$arrstr);

Use json_decode method to get back the urlData and is_admin in app.blade.php like

<?php 
    $permission = session('permission');
    $perArray = json_decode($permission[0],true);
    $urlData = $perArray['urlData']; $is_admin = $perArray['is_admin']; 
?> 


Source: stackoverflow