Closed ricardogomez-dev closed 3 years ago
You can run ajax request after updating permission over vue and pass returned data to JS permission variabe:
axios({ method: 'get', url: 'get-permissions' / put full laravel route to get permissions / }) .then(res => { window.Laravel.jsPermissions = res.permissions }) .catch(function (e) { let res = e.response; if (res) { switch(res.status) { case 401: // handle error code break; default: // handle error code } } })
In laravel side:
Route::get('/get-permissions', function () { $permissions = auth()->check()?auth()->user()->jsPermissions():null; if($permissions){ $permissions = json_decode($permissions); } return response()->json(['permissions' => $permissions], 200); });
Hey @ricardogomez-dev, where do you implement the axios function? I cant make it work
@AARP41298 I'm using Laravel Jetstream with Inertia.js, I resolved it with shared data at HandleInertiaRequests class: https://inertiajs.com/shared-data
And then when mounting the component I assign the permissions to the window.Laravel.jsPermissions object.
mounted(){ window.Laravel.jsPermissions = this.$page.props.permissions }
If you are using other stack or still having troubles I can gladly help you, write me to my email: Ricardogomez95@outlook.com
thank you so much @ricardogomez-dev, im using inertia too and it works perfectly the only tweak that remains to implement, is on my admin layout, to avoid call the window.laravel on every view
PD te la rifaste fernando, i mean, ricardo
@ricardogomez-dev I tried all the options above and they gave no results, I login and only after F5 that works, I saw that the welcome page is not updated.
Laravel 8 + Inertiajs + vue3
After Login
After F5
@multiarts Veo que eres del Salvador entonces asumo que hablas español, con gusto te puedo ayudar, me puedes enviar screenshots de tu código para ver si hay algún error a mi email: ricardogomez95@outlook.com
@ricardogomez-dev si amigo, si tambien tienes telegram podemos hablar.
@AARP41298 I'm using Laravel Jetstream with Inertia.js, I resolved it with shared data at HandleInertiaRequests class: https://inertiajs.com/shared-data
And then when mounting the component I assign the permissions to the window.Laravel.jsPermissions object.
mounted(){ window.Laravel.jsPermissions = this.$page.props.permissions }
If you are using other stack or still having troubles I can gladly help you, write me to my email: Ricardogomez95@outlook.com
Hola Ricardo, logro hacer la primera parte, pero el mounted no logro identificar donde ponerlo, será en el AppLayout.vue?
No logro hacer el mounted con
HandleInertiaRequest.php
public function share(Request $request)
{
return array_merge(parent::share($request), [
'permissions' => json_decode(auth()->check() ? auth()->user()->jsPermissions() : '{}',true),
'ziggy' => function () use ($request) {
return array_merge((new Ziggy)->toArray(), [
'location' => $request->url(),
]);
},
]);
}
AppLayout.vue
<script setup>
import { ref, onMounted } from 'vue';
import { Inertia } from '@inertiajs/inertia';
import { Head, Link } from '@inertiajs/inertia-vue3';
import JetApplicationMark from '@/Components/ApplicationMark.vue';
import JetBanner from '@/Components/Banner.vue';
import JetDropdown from '@/Components/Dropdown.vue';
import JetDropdownLink from '@/Components/DropdownLink.vue';
import JetNavLink from '@/Components/NavLink.vue';
import JetResponsiveNavLink from '@/Components/ResponsiveNavLink.vue';
defineProps({
title: String,
});
const showingNavigationDropdown = ref(false);
const switchToTeam = (team) => {
Inertia.put(route('current-team.update'), {
team_id: team.id,
}, {
preserveState: false,
});
};
const logout = () => {
Inertia.post(route('logout'));
};
</script>
In AppLayout.vue
``
How to update the permissions in a SPA without refreshing the browser? I'm working with a SPA and when I login to the app I got error because the user don't get the permissions, I have to refresh the browser to get the user permissions but that's not the point in a SPA.