K
K
Kristina87872020-09-14 14:33:30
Laravel
Kristina8787, 2020-09-14 14:33:30

Why does Auth::user return null?

Good day. Faced such a problem, it is necessary that when going through the 'admin' route, the controller checks whether the user is authorized in the system, and if so, returns the user to the variable and gives access to the page, otherwise an error occurs. But Auth::user constantly gives me null, although the data is entered correctly and I remain authorized on the site, but Auth::user shows null. I'm new to Laravel, I don't understand why, here is the router code

Auth::routes();

Route::get('login', 'Auth\[email protected]');

Route::post('login', 'Auth\[email protected]');

Route::get('logout', 'Auth\[email protected]');

//Admin

Route::group(['prefix' => 'admin','middleware'=> 'auth'],function() {

    //admin
    Route::get('/',['uses' => 'Admin\[email protected]','as' => 'adminIndex']);

    Route::resource('/articles','Admin\ArticlesController');

});


and here is the controller

public function __construct()
    {
        $this->user = Auth::user();

        if(!$this->user){
            abort(403);
        }
    }


and I'm getting a 403 error all the time as you can see. Help me to understand

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2020-09-14
@Kristina8787

Section https://laravel.com/docs/7.x/controllers#controlle... , last two paragraphs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question