1

I follow the video Multiple Authentication in Laravel 5.4 Natively! (Admins + Users) - Part 1
From part1 to 5, it's all done. Now I can login, logout.
Buy when I want to get the admin's properties, I got problem.

In admin blade, I can use {{ Auth::user()->name }}, it's ok.
In Composer, it's also ok.

<?php

namespace App\Http\ViewComposers;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Auth;

class AdminComposer
{
  /**
   * Create a movie composer.
   *
   * @return void
   */
  public function __construct(Request $request)
  {
    $this->admin= Auth::user();
    $this->base = config('app.url') . '/admin/';

  }

  /**
   * Bind data to the view.
   *
   * @param  View  $view
   * @return void
   */
  public function compose(View $view)
  {
    $view->with('admin', $this->admin);
    $view->with('base', $this->base);
  }
}

Then I try to use View in Controller, there is nothing, and no error message.

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Auth;

class Controller extends BaseController
{
  use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

  public function __construct()
  {
    $admin = Auth::user();
    echo "<pre>", print_r($admin, 1), "</pre>"; exit;
    if(!empty($admin)){
      View::share('admin', $admin);
    }
  }
}

Why Auth::user() is ok in blades and composer, not working in controller ?


/config/auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],

    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Models\Admin::class,
    ],
],

Maybe I found the reason. Though still not resolved.

In laravel's Controller.php, it's not working. But in any other controller I created, it's ok

For example, the following is ok.
App\Http\Controllers\Admin\HomeController.php

public function __construct()
{
  $this->middleware('auth:admin');
  parent::__construct();
}

public function index()
{
  $admin = Auth::user();
  dd(Auth::user());
}

Maybe this is because the middleware.

In other post, someone uses Auth::guard('mrm')->User()
But I do the same, I tried Auth::guard('admin')->User()
Still nothing...

6
  • What happens when you dd(Auth::user())? Commented Mar 24, 2018 at 18:53
  • What the error ? Commented Mar 24, 2018 at 18:56
  • are u missing $this->$admin = Auth::user(); in controller Commented Mar 24, 2018 at 18:57
  • try the auth()->user() from the global helper Commented Mar 24, 2018 at 19:18
  • dd(Auth::user()) and dd(auth()->user()) are both null Commented Mar 24, 2018 at 20:28

2 Answers 2

4

Since Laravel 5.3 you are no longer able to access session in controller constructors, because middleware has not run yet. Authenticating a user, or getting the authenticated user requires session middleware to have already run.

You can define a closure (scroll to "Session In The Constructor") that happens after the session middleware has run.

Sign up to request clarification or add additional context in comments.

Comments

1

I typically use Illuminate\Support\Facades\Auth as the pulled in namespace. Try that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.