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

Unable to authorize users using middleware #66

Open
andrzejkupczyk opened this issue Feb 1, 2022 · 1 comment
Open

Unable to authorize users using middleware #66

andrzejkupczyk opened this issue Feb 1, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@andrzejkupczyk
Copy link

andrzejkupczyk commented Feb 1, 2022

What is the issue?

  1. I'm not able to limit access to the /health endpoint using middleware. There's no way to retrieve the user making the request ($request->user() returns null). Am I missing something?
  2. Not sure if it is possible to use middleware with parameters (e.g. role:admin)?

Steps To Reproduce

Use existing middleware, e.g.:

    'middleware' => [
        \App\Http\Middleware\Authenticate::class,
    ],

or try with a custom one (try to dump the user) and visit the /health endpoint.

Zrzut ekranu z 2022-02-01 16-44-27

As you can see I was redirected to the login page, but since I was already logged in, the app redirected me back to the home page.

Expected behaviour

If the user is authenticated, the user object should be accessible so I could make some additional authorization checks.

Additional context

Works fine when the middleware parameter is empty or contains a middleware which does not try to access the user object.

Tested with Laravel 8.76.1.

@andrzejkupczyk andrzejkupczyk added the bug Something isn't working label Feb 1, 2022
@Gman98ish
Copy link
Contributor

Sorry, just saw this @andrzejkupczyk

You might also need to add the StartSession middleware first, potentially the cookie ones as well

The /health endpoint has no middleware on it by default, so might be an idea to copy/paste the 'web' defaults:

https://github.com/laravel/laravel/blob/9.x/app/Http/Kernel.php#L35

I'll have a think about how better to solve this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants