ahmedsaoud31 / laravel-permission-to-vuejs

Laravel Permission Package To Use it in VueJs
87 stars 27 forks source link

Permissions doesn't work in a SPA #4

Closed ricardogomez-dev closed 3 years ago

ricardogomez-dev commented 3 years ago

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.

ahmedsaoud31 commented 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); });

AARP41298 commented 3 years ago

Hey @ricardogomez-dev, where do you implement the axios function? I cant make it work

ricardogomez-dev commented 3 years ago

@AARP41298 I'm using Laravel Jetstream with Inertia.js, I resolved it with shared data at HandleInertiaRequests class: https://inertiajs.com/shared-data

Captura de Pantalla 2021-06-25 a la(s) 22 04 39

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

AARP41298 commented 3 years ago

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

multiarts commented 3 years ago

@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 1_erro 2_erro

After F5 after_f5

ricardogomez-dev commented 3 years ago

@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

multiarts commented 3 years ago

@ricardogomez-dev si amigo, si tambien tienes telegram podemos hablar.

flprblr commented 1 year ago

@AARP41298 I'm using Laravel Jetstream with Inertia.js, I resolved it with shared data at HandleInertiaRequests class: https://inertiajs.com/shared-data

Captura de Pantalla 2021-06-25 a la(s) 22 04 39

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 `