Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auth::guard('user')->user() is null in Laravel 5.3

I'm trying to pass to all templates current user object like this:

class Controller extends BaseController
{
    public function __construct()
    {
        view()->share('usr', Auth::guard('user'));
    }
}

Every controller is extended by Controller. But if i try to dump Auth::guard('user')->user() Laravel returns null, although I am logged in. Moreover when i pass this variable into template, {{ $usr->user() }} returns current user. What I've done wrong?

my config/auth.php

'defaults' => [
    'guard' => 'user',
    'passwords' => 'users',
],
'guards' => [
        'user' => [
            'driver' => 'session',
            'provider' => 'user',
        ],
    ],
'providers' => [
        'user' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    ],

Kernel.php

         protected $middleware = [
     \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
 ];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        //\App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

my own function to log in:

public function authorizes(Request $request)
{
        $this->validate($request, [
        'login' => 'required|max:50',
        'password' => 'required|max:50'
    ]);

        $credentials = $request->only(['login', 'password' ]);
        $remember = $request->get('remember', false) == 1 ? true : false;

    if ($this->guard->attempt( $credentials, $remember)) {
        $user = $this->guard->user();
        $user->last_login = date('Y-m-d H:i:s');
        $user->save();
        return redirect()->route( 'homepage' )->withSuccess(trans('app.login.success'));
    }
    return redirect()->back()->withErrors(trans('app.wrong.credentials'));
}
like image 730
jdoe Avatar asked Nov 15 '16 15:11

jdoe


People also ask

What is Guard () in Laravel?

Guards define how users are authenticated for each request. For example, Laravel ships with a session guard which maintains state using session storage and cookies. Providers define how users are retrieved from your persistent storage.

What is Auth :: Routes () in Laravel?

Auth::routes() is just a helper class that helps you generate all the routes required for user authentication. You can browse the code here https://github.com/laravel/framework/blob/5.3/src/Illuminate/Routing/Router.php instead.


1 Answers

In Laravel 5.3 you should change your controller constructor like so to make this work (assuming you use at least Laravel 5.3.4):

public function __construct()
{
    $this->middleware(function ($request, $next) {
        view()->share('usr', Auth::guard('user'));

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

You can see this change described in Upgrade guide:

In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4

like image 128
Marcin Nabiałek Avatar answered Sep 28 '22 06:09

Marcin Nabiałek