php - laravel 5.4 auth facade does not work -
the laravel 5.4 auth facade not work. auth middleware functional normally. call protected route produces login page, expected. malformed credentials produce expected error. correct credentials produce correct page, expected. commenting out line "use illuminate\support\facades\auth;" produces error "class 'app\http\controllers\admin\auth' not found", expected.
specifically, auth::check() produces false, , auth::user() returns nothing, in spite of successful login. calls either of these implementations of auth facade not produce error.
this code worked in laravel 5.2. deviation procedure of aware during migration laravel 5.4, did not run command "php artisan make:auth" in new laravel implementation.
a search "laravel 5.4 auth facade not work" not produce relevant results, either in bing or in stackoverflow internal search engine.
pertinent code displayed, below:
namespace app\http\controllers\admin; use illuminate\http\request; use app\http\requests; use app\http\controllers\controller; use app\user; use app\models\admin; use app\models\role; use app\models\role_user; use app\models\client; use app\models\contact; use app\models\publicpages; use app\models\survey_question; use app\models\survey_item; use app\models\survey_project; use app\models\state; use app\models\test_post; use app\models\registration; use hash; use redirect; use db; use illuminate\support\facades\auth; use app\classes\rolehelper; use app\classes\commoncode; class admincontroller extends controller { var $obj_logged_in_user; var $arr_logged_in_user; var $bool_has_role; var $rolehelper; //organization of function // 1. __construct // 2. index // get, alphabetically // post, alphabetically public function __construct( role_user $role_user, rolehelper $rolehelper, request $request) { $this->middleware('auth'); echo "<pre>"; echo "admin controller, line 50<br>"; print_r($request->user); echo "</pre>"; // line 49 (request->user) produces nothing if (auth::check()) { $this->middleware('role:admin'); $this->obj_logged_in_user = auth::user(); print_r($this->obj_logged_in_user); // line 58 (print_r) produces nothing $this->arr_logged_in_user = $rolehelper->prepare_logged_in_user_info($this->obj_logged_in_user); } // end if auth::check() } // end __construct function
in conclusion, auth::check() return accurate results, , auth::user() return logged in user object. thank in advance.
if want use auth
functions inside __construct()
method need pass $this->middleware()
inside closure.
public function __construct(role_user $role_user, rolehelper $rolehelper) { $this->middleware('auth'); $this->middleware('role:admin'); $this->middleware(function ($request, $next) use ($role_user, $rolehelper) { if (auth::check()) { $this->obj_logged_in_user = auth::user(); $this->arr_logged_in_user = $rolehelper->prepare_logged_in_user_info($this->obj_logged_in_user); } return $next($request); }); }
also, fyi, might find easier use dump()
or dd()
functions come laravel. instead of writing:
echo "<pre>"; echo "admin controller, line 50<br>"; print_r($request->user); echo "</pre>";
you instead write:
dump('admin controller, line 50', $request->user);
https://laravel.com/docs/5.4/helpers#method-dd
hope helps!
Comments
Post a Comment