Skip to content
Advertisement

Laravel Auth guard does not persist after redirect CustomUser

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

Advertisement

Answer

The problem was that I didn’t return when implementing IlluminateContractsAuthUserProvider the object from

public function retrieveById($identifier)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement