0
votes

After redirecting from LoginController Auth::guard('user')->user() returns null

 public function userPostLogin(array $collection)
{
    return Auth::guard('web')->attempt($collection);

}

LoginConsumer

  public function userLoginAuthentication(Request $request)
{
    if ($request->get('phoneNumber') != null && $request->get('password') != null) {
        $login = new LoginDTO($request->get('regionCode'). ' ' .$request->get('phoneNumber'), $request->get('password'), $request->get('role'));
        $mapper = new LoginMapper();
        return $this->userLoginConsumer->userPostLogin((array)$mapper->objectToCollection($login));}

LoginService

public function loginAuthentication(Request $request)
        {
            $this->userLoginService->userLoginAuthentication($request);
                    return redirect()->route('user-home');

LoginController

Auth::guard('web')->user() returns the right user in each of this layers. But on redirect to route('user-home') (or to any route) the same Auth::guard('web')->user() returns null

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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins'
        ],
        'admin-api' => [
            'driver' => 'token',
            'provider' => 'admins'
        ],

        'kitchen' => [
            'driver' => 'session',
            'provider' => 'kitchens'
        ],
        'kitchen-api' => [
            'driver' => 'token',
            'provider' => 'kitchens'
        ]
    ],

'providers' => [
        'users' => [
            'driver' => 'jsonresponse',
            'model' => CustomUser::class,
        ],

        'admins' => [
            'driver' => 'jsonresponse',
            'model' => CustomUser::class,
        ],

        'kitchens' => [
            'driver' => 'jsonresponse',
            'model' => CustomUser::class,
        ],

 'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    'password_timeout' => 10800,

auth.php

2
are all of these routes in the web.php file?lagbox
they are all in user.php (custom route file)InFo55
did you apply the 'web' middleware to these routes?lagbox
Route::group(['middleware' => 'web'], function () { Route::get('/home', 'HomeController@index')->name('user-home'); });InFo55
Do you call Auth::guard('web')->user() in your controller?azibom

2 Answers

0
votes

You can get the user like this after your auth process like this:

Auth::user();

And you can also check like this:

if (Auth::check()) {
    $user = Auth::user();
} else {
    echo "We do not have a authenticated user";
}

And you should apply the auth middleware to these routes

0
votes

The problem was that I didn't return when implementing Illuminate\Contracts\Auth\UserProvider the object from

public function retrieveById($identifier)