Support the ongoing development of Laravel.io →
Vue.js Laravel JavaScript

emohammad liked this thread

1

Hello You should use a state manager if you need to check permissions from frontend. I'm developing an app with same stack. I'm using pinia for frontend permission checks. Ofcourse you need to check permissions on backend eitherway. How i succeed in doing it is;

  • I'm sending permissions over with middleware (HandleInertiaRequests middleware, as in described in inertia docs)
  • I'm using lazy evolution to be sure it's sent over only once ( again, described in documentation)
  • I've created a store which loads permissions and check with some functions. Here is how my store looks like: ` import { defineStore } from 'pinia';

export const useYetkilerStore = defineStore('yetkiler', { state: () => { const yetkiler = []; const roller = []; return { yetkiler, roller, } }, getters: { can (state) { return (yetkiadi) => { if ( state.yetkiler && state.yetkiler.indexOf(yetkiadi) !== -1 || state.roller.indexOf('admin') > -1

    ) {
      return true;
    }
    return false;
  }
},
any (state) {
  return (yetkiler) => {
    if ((state.yetkiler && state.yetkiler.filter(item => yetkiler.indexOf(item) !== -1).length)  || state.roller.indexOf('admin') > -1) {
      return true;
    }
    return false;
  }
},
all (state) {
  return (yetkiler) => {
    if ((state.yetkiler && state.yetkiler.filter(item => yetkiler.indexOf(item) !== -1).length === yetkiler.length)  || state.roller.indexOf('admin') > -1) {
      return true;
    }
    return false;
  }
},
is (state) {
  return (roleadi) => {
    if (state.roller && state.roller.indexOf(roleadi) !== -1) {
      return true;
    }
    return false;
  }
}

}, actions: { setYetkiler (yetkiler) { this.yetkiler = yetkiler; }, setRoller (roller) { this.roller = roller; } } }); ` yetkiler = abilities roller = roles

I'm loading permissions and roles from main layout:

setup(){ onBeforeMount(() => { yetkilerStore.setRoller(window.roles); yetkilerStore.setYetkiler(window.abilities); }) }

I'm using window object instead of something else because i want to be sure that i'm loading all permissions before anything else is mounted.

It's my first post. Be easy on me :)

0

@gokhansarigul have you took a look into inertia remember feature? It suppose to do the trick without the need to use Pinia.

Last updated 1 year ago.
0

As i understood "Remember" feature is used for browser history features. It doesnt look like a state manager, but more like to keep form filled in case of history.back()

emohammad liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.