Get authenticated user in constructor

Asked

Viewed 235 times

1

After a lot of research and taking a look at documentation, I fully understand that and why can not do the "old fashioned", but wanted a solution/ workaround to do the following:

I have a base class, it is not called directly in the request, it is not the one that is declared in the routes:

class AdminPanelController extends Controller
{
    protected $_authUser;

    public function __construct() {
        $this->middleware(function ($request, $next) {
             $this->_authUser = Auth::guard('admin')->user();
             dd($this->_authUser); // tudo bem
             return $next($request);
        });
    }
    ...
    protected function yoo() {
        dd($this->_authUser); // null, mas precisava disto aqui
    }
}

Needed the authenticated user to be available in the method yoo(), and the controller called directly with the request:

Route::get('users', 'UsersController@hey');

Userscontroller:

class UsersController extends AdminPanelController 
{

    protected params;

    public function __construct(Request $request) {
        parent::__construct();
        $this->params = $this->yoo(); // auth user é null, mas precisava de chamar o metodo aqui
    }
    ...
}

Note that if you call the method $this->yoo() in a place other than the builder already works well, but I really need to call in the builder.

NOTE: I also tried $request->user() with the result Authentication user provider [] is not defined., as this is a multiauthentication system would have to define a Guard: $request->guard('admin')->user() that gave: Method guard does not exist

  • Because it is being assigned within the method middleware?

  • @Virgilionovic, I honestly saw this solution by the Docs that are on the link. But it didn’t help me much

  • @Virgilionovic , the problem is not with the middleware that checks whether or not it is authenticated, this works perfectly

  • yes yes I understood, I did not also say that it is a problem only I questioned why to do it within the middleware Then you explained that you are in the documentation for Laravel 5.3.4.

  • 1

    @Virgilionovic, but as I say to you, it seems to me that with this or without it is the same. I will add an extra information to the question

  • It doesn’t really work, I assume from the premise that the middleware runs first and in this case no, I may be even mistaken, but, I tested the codes and do not have access to construtor only in the methods when the same one already executed that middleware. So much so that if I put in the constructor of the class Adminpanelcontroller on the last line echo $this->_authUser; has nothing, because it has not yet been executed. The documentation in some cases leaves to be desired, unfortunately.

  • 1

    But very obnoxious @Virgilionovic, for trying to help

Show 2 more comments

1 answer

0

Try something like that:

class AdminPanelController extends Controller
{
    protected $_authUser;

    public function __construct() {
        $this->middleware([$this, 'getAuthUserFromMiddleware']);
    }

    public function getAuthUserFromMiddleware($request, $next){
        $this->_authUser = Auth::guard('admin')->user();
        return $next($request);
    }

    protected function yoo() {
        return $this->_authUser;
    }
}

class UsersController extends AdminPanelController 
{
    protected params;

    public function __construct(Request $request) {
        parent::__construct();
        $this->params = $this->yoo();
    }
}

Or so putting the use in function:

class AdminPanelController extends Controller
{
    protected $_authUser;

    public function __construct() {
        $this->middleware(function ($request, $next) use($this->_authUser) {
             $this->_authUser = Auth::guard('admin')->user();
             return $next($request);
        });
    }

    protected function yoo() {
       return $this->_authUser;
    }
}
  • No, and this last one gives syntax error, because of the "->" inside the use. But Obrgado for trying

Browser other questions tagged

You are not signed in. Login or sign up in order to post.