Search code examples
phplaravelcartalyst-sentinel

Laravel 5.1 Sentinel::getUser() return null


I am using Sentinel to authenticate users and as the auth Middleware.

Middleware code:

public function handle($request, Closure $next)
{
    var_dump(Sentinel::guest()); // prints false
    if (Sentinel::guest()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('/login');
        }
    }

    return $next($request);
}

Controller code:

public function getAccount() {
    var_dump(Sentinel::guest()); // prints true
    return Sentinel::getUser();
}

routes.php

Route::group(['middleware' => ['auth']], function () {
    Route::get('api/v1/temp/users/account', 'App\Http\Controllers\UsersController@getAccount');
}

Then if i browse to api/v1/temp/users/account the var_dump() in the Middleware is printing false, while the var_dump() inside the controller is printing true which seems a nonsense.

What's wrong?


Solution

  • It turned out i was using the native facade instead of the Laravel facade Cartalyst\Sentinel\Laravel\Facades\Sentinel. That fixed.