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
User is always null? #32
Comments
I'm seeing the same thing. |
Same issue. The gate doesn't seem to work at all. |
I guess you're not logged in, since you're trying to get the email address of a non existing user. Just tested it, and it seems that nothing is wrong with the package, since this works: Auth::login(factory(User::class)->create([
'email' => 'ruben@spatie.be'
]));
Gate::define('viewWebTinker', function ($user = null) {
return $user->email === 'ruben@spatie.be';
}); Will close this for now. |
I could also write a test that would pass. It doesn’t mean that it actually works in real life. I assure you I am logged in. |
I'm also sure I'm logged in as I clicked on it in |
I'm also sure I'm logged in and have the same exact behavior. With |
@repat I didn't change the path. Something is definitely broken. I'll fork it and figure it out this weekend. As good as testbench is, the "it works fine in my tests but doesn't work when pulled into an actual Laravel app" thing isn't a rare problem. |
I think this might be a Laravel bug actually. It seems that Laravel isn't passing the user to the Gate closure as documented. I wonder if it has to do with the fact that this package explicitly defines its own middleware and does not also use the web middleware. I vaguely remember having a problem like this with another package when I upgraded a site to Laravel 6 and I worked around it by adding the web middleware to the route along with the package's own middleware. I could be wrong and don't have time to dig into it today but I'll experiment over the weekend. |
Yup. That's definitely it. If I use the same gate check in a route that also has the web middleware the user model exists as expected. So I guess maybe we need to ask @rubenvanassche if he and the other Spatie developers think this should be handled via Laravel or here. |
If it defines it's own middleware shouldn't that be documented, since it's not using the 'default' web middleware? |
Not necessarily. The Laravel documentation doesn't say that the user will only be provided to the Gate closure if the web middleware is used, though that does seem to be the behavior. I'm not sure who's actually at fault here. |
In the mean time, this will get anyone who's stuck past the problem. Do this in your web.php route file:
Obviously use anything you like in the prefix as long as it does not match the path set in the web tinker config file. |
Hi @edgrosvenor, thanks for your extensive research into this problem! Indeed adding the So I've added them(c693f3f) and tagged a new release! Thanks guys! |
If I remove
= null
I get a 403. Am I doing something wrong here or is this part broken?The text was updated successfully, but these errors were encountered: