0
votes

The session is being destroyed after successfully logged in or there's some error with the guard that couldn't retain the session. When asked for your_session_key the on the view of the dashboard, it provides null.

Route::group(['prefix' => 'admin'], function () {
Route::namespace('Admin')->group(function () {
Route::group(['middleware' => ['admin_middle','auth:admin']] , function () {
        Route::get('accounts/', 'AccountsController@index')->name('admin.accounts');
        });
    });
});

Middleware: App\Http\Middleware\RedirectIfNotAdmin //Registered in Kernel as 'admin_middle' => \App\Http\Middleware\RedirectIfNotAdmin::class,

class RedirectIfNotAdmin
{
public function handle($request, Closure $next, $guard = 'admin')
{
    if (!auth()->guard($guard)->check()) {
        $request->session()->flash('error', 'You must be an Admin to see this page');
        return redirect(route('auth.admin.login'));
    }

    return $next($request);
}
}

Guard: config/auth.php // Custom Guard

'guards' => [
'admin' => [
    'driver' => 'session',
    'provider' => 'admin',

],
],

AccountsController: Controllers\AccountsController

class AccountsController extends Controller {

public function __construct(AdminRepositoryInterface $adminRepository) {
    $this->adminRepo = $adminRepository;
}

private $adminRepo;
public function index(int $id)
{
    $admin = $this->adminRepo->findAdminById($id);

    $talentRepo = new AdminRepository($admin);


    return view('admin.accounts');
}

}

AdminRepositoryInterface: App\Shop\Admins\Repositories\Interfaces\AdminRepositoryInterface;

interface AdminRepositoryInterface extends BaseRepositoryInterface
{
public function findAdminById(int $id) : Admin;
}

AdminRepository: App\Shop\Admins\Repositories\AdminRepository

class AdminRepository extends BaseRepository implements AdminRepositoryInterface
{
public function findAdminById(int $id) : Admin
{
    try {
        return $this->findOneOrFail($id);
    } catch (ModelNotFoundException $e) {
        throw new AdminNotFoundException($e);
    }
}
}

View: admin\accounts.blade

@if (Session::has('YOUR_SESSION_KEY'))
{{-- do something with session key --}} 
@else
{{-- session key does not exist  --}} //this has been printed is the ID variable is not passed
@endif
{{$admin->name}}
 <br />{{$admin->email}}
1
There is a lot of code in the question that doesn't relate to the problem. @param AdminRepositoryInterface $adminRepository Is the adminRepo object being passed as a parameter and being ignored? What are you expecting to populate $this->adminRepo?AD7six
I'm expecting the value from the session. @AD7sixNaren
I think you have misunderstood the question Naren.AD7six
I guess, I've understood a little. I've updated a code. Now its different error @AD7sixNaren
I don't know I feel like there's some error with the guard. @AD7six and middleware. Please suggest something.Naren

1 Answers

1
votes

It's saying $this->adminRepo returns null.

Try initializing your $this->adminRepo in the controller's constructor. If you typehint the interface, make sure you bind it in a service provider.

https://laravel.com/docs/5.8/container#binding-basics