Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single Action Controllers ( __invoke() ) constructor not authenticating session #78

Closed
kharysharpe opened this issue Feb 26, 2020 · 2 comments

Comments

@kharysharpe
Copy link

  • Airlock Version: v0.2.1
  • Laravel Version: v6.17.0
  • PHP Version: 7.1
  • Database Driver & Version:

Description:

Not sure if I missed something, if it's by design or a bug.

Steps To Reproduce:

class TestController extends ApiController
{
    public function __construct(Request $request)
    {
        dd($request->user()); // null;
    }

    public function __invoke(Request $request)
    {
        dd($request->user()); // App\User;
    }
}

How do i get the signed in user in the constructor?

@rcubitto
Copy link

It's by design since the session hasn't been loaded yet. Here's a workaround using the middleware helper method.

public function __construct()
{
    $this->middleware(function ($request, $next) {
        $request->user(); // your logic here...

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

@kharysharpe
Copy link
Author

It worked, thanks @rcubitto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants