[2.x] Change $page.user to $page.auth.user #317#323
Conversation
drjdr
commented
Oct 7, 2020
- Changing $page.user to $page.auth.user makes authenticated user consistent with intertia.js examples.
- Prevents mixing the auth.user with a user object returned from backend.
|
Major breaking change for little benefit? |
|
It will require a quick code change for the early adopters indeed.
I understand that this change in the crucial variable name is going to break stuff, but hence the [2.x] label update:
|
|
I just had this same issue, while trying out Jetstream with Inertia and creating a user list and detail page as a HelloWorld app. I understand this might be a breaking change now, but I'm willing to bet a lot people will encounter this. I didn't even notice it at first, only when I saw the name of the current logged in user being changed in the navigation bar. I think an authUser or auth.user prop would prevent this issue and be more consistent with the Laravel backend auth user. |
|
What about made these changes as a config? Or just allow publishing InertiaSharedData to make it easier to make these changes |
|
@taylorotwell @driesvints @reinink May I kindly ask to reconsider to add this in v2? Having to substitute |
|
For your info, @taylorotwell and I had a chat about this yesterday, and I think we're going to make this change after-all. Taylor was (rightfully) trying to avoid a breaking change here, but the reality is we've already got a breaking change, since the Both @claudiodekker and myself are working hard to update both Inertia and the Vue adapters in preparation for the Jetstream 2.0 release. We'll also be making some PRs against Jetstream (likely today at some point) to get everything in sync and ready to go. |
|
Thanks @reinink! |
|
Thanks a lot to all of you and the work you do for this project! |
|
Hi @reinink @claudiodekker ,
|
|
@reinink Did you choose not to implement this change for V2 after all? I can see in the ShareInertiaData middleware that the User prop has not been moved to a new auth property. |
|
@markvesterskov UGH, we totally forgot! 🤦 @taylorotwell Probably too late for Jetstream V2, yes? Or do you think it would be possible to still make this change? 😬 |
|
Please do! I think this issue will be reposted every few weeks otherwise, I've seen a few others like this already |
|
@reinink I noticed because of me overwriting the user model in a controller today making everything blow up. I reeeheeeeeally hope that this can still make it in! If not I’ll have to hack it together myself and that would be a shame. |
|
It's really @taylorotwell's call at this point, if he feels like this makes sense to bring to V2 still. I suspect it's actually fine, since once you install Jetstream, it's "done". Meaning, if you've installed V2 already, you'll get the current behaviour. But if we update it to do I don't think anything in the docs actually refers to |
|
@reinink if we make the change the next time an existing application updates their Composer dependencies their templates will break. I'm OK with making the update (I've heard enough complaining about Jetstream so what's a little more?) but we will need to make a post explaining that people need to make the change in their templates. |
|
Is this still under review? Or did something actually occur |
|
Any update on this? I really would like to see this getting updated. :-) |
|
Hi @driesvints, I propose to reopen this issue and maybe affected users can attempt to PR. |
|
We can't do this change in Jetstream 2.x for reasons explained above. Please attempt this PR to master. |
|
Well noted Dries. I will attempt the PR to master. Thanks. In the meantime and for anyone who may stumble this issue in future, here is how I worked around. I used the container to bind modified copy of the middleware as follows: <?php
namespace App\Providers;
use App\Http\Middleware\ShareInertiaData;
use Illuminate\Support\ServiceProvider;
use Laravel\Jetstream\Http\Middleware\ShareInertiaData as JetstreamShareInertiaData;
class InertiaDataServiceProvider extends ServiceProvider
{
public $bindings = [
JetstreamShareInertiaData::class => ShareInertiaData::class,
];
}<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Session;
use Inertia\Inertia;
use Laravel\Fortify\Features;
use Laravel\Jetstream\Jetstream;
class ShareInertiaData
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param callable $next
* @return \Illuminate\Http\Response
*/
public function handle($request, $next)
{
Inertia::share(
array_filter([
'jetstream' => function () use ($request) {
return [
'canCreateTeams' => $request->user() &&
Jetstream::hasTeamFeatures() &&
Gate::forUser($request->user())->check('create', Jetstream::newTeamModel()),
'canManageTwoFactorAuthentication' => Features::canManageTwoFactorAuthentication(),
'canUpdatePassword' => Features::enabled(Features::updatePasswords()),
'canUpdateProfileInformation' => Features::canUpdateProfileInformation(),
'hasEmailVerification' => Features::enabled(Features::emailVerification()),
'flash' => $request->session()->get('flash', []),
'hasAccountDeletionFeatures' => Jetstream::hasAccountDeletionFeatures(),
'hasApiFeatures' => Jetstream::hasApiFeatures(),
'hasTeamFeatures' => Jetstream::hasTeamFeatures(),
'hasTermsAndPrivacyPolicyFeature' => Jetstream::hasTermsAndPrivacyPolicyFeature(),
'managesProfilePhotos' => Jetstream::managesProfilePhotos(),
];
},
'auth' => [
'user' => function () use ($request) {
if (!$request->user()) {
return;
}
if (Jetstream::hasTeamFeatures() && $request->user()) {
$request->user()->currentTeam;
}
return array_merge(
$request->user()->toArray(),
array_filter([
'all_teams' => Jetstream::hasTeamFeatures() ? $request->user()->allTeams()->values(
) : null,
]),
[
'two_factor_enabled' => !is_null($request->user()->two_factor_secret),
]
);
},
],
'errorBags' => function () {
return collect(optional(Session::get('errors'))->getBags() ?: [])->mapWithKeys(
function ($bag, $key) {
return [$key => $bag->messages()];
}
)->all();
},
])
);
return $next($request);
}
}PS: Note that by overriding a the above class, you are responsible to track changes upstream. You also need to update |