0

I put myRoute inside the auth middleware group and I get

// myController
Auth::guard('web')->user()  // null

I dont know what to do.. I have controllers and routes below.

Seems like parent controller constructor is working but the middleware is not.

// Controller.php
public function __construct() {
    $this->middleware(function ($request, $next) {
        $this->currentUser = Auth::guard('web')->user();

        return $next($request);
    });
}

// myController extends Controller
public function __construct(Request $request) {
    parent::__construct();
    $this->request = $request;
    var_dump($this->currentUser); // null
    var_dump(Auth::guard('web')->user); // null
    // $userId = Auth::guard('web')->user()->id;
    // $userId = $this->currentUser->id;
    $this->userId = $userId;
}

// route/web.php
Route::middleware(['auth'])->group(function () {
    Route::get('myRoute', 'myController@index');
}
2
  • 2
    You don't need to specify guard('web') since, by default laravel assumes web guard. Commented Aug 31, 2017 at 4:20
  • I have same problem. can you share code? how work it? Commented Oct 16, 2018 at 10:13

1 Answer 1

3

You can't get the Auth::user() in constructor. try Auth::guard('web')->user(); in another method. The result is not null anymore

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

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.