简体   繁体   中英

Laravel 6 - Auth::guard('user')->user return null

I create a multiple Authentication in Laravel. When I login with user, on debug in post login method, after Auth::guard('user')->attempLogin..... I see a user but after redirect to HomeController this return null.

How to resolve? I'm beginner in Laravel.

Thank's!!!

/routes/auth/user.php

Route::prefix('backoffice')->name('user.')->namespace('User')->middleware('user')->group(function () {
Auth::routes();
Route::get('home', 'HomeController@index')->name('home');

});

/routes/web.php

Route::group(['middleware' => 'web'], function() {

require 'auth/user.php';

Route::get('/', function () {
    return view('welcome');
});
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('cadastro', 'CadastroController');

});

/app/Controllers/User/Auth/LoginController - @post Login

public function login(Request $request) {
    $credentials = [
        'username' => $_POST['username'],
        'password' => $_POST['password']
    ];

    Auth::guard('user')->attempt($credentials, false);


    //dd('auth', Auth::guard('user'));

    return redirect()->intended('/backoffice/home');

}

/app/Controllers/User/HomeController

public function __construct()
{
    $this->middleware('user');
    dd('after middleware', Auth::guard('user'), Auth::guard('user')->user());

}


public function index()
{
    return view('user.home');
}

By default, Laravel doesn't ship with auth guard user . Perhaps you meant to use web guard ie Auth::guard('web') . Auth::guard()->user() should return the logged in user object if a user is logged in.

Also, the default middleware for checking logged in user is auth , not user . So, your route might look like this: Route::prefix('backoffice')->name('user.')->namespace('User')->middleware('auth')->group(function () {}); , except you've defined a custom middleware in app/Http/Kernel.php $routeMiddleware array with alias user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM