I have set up the auth for my backend to protect t...
# permit-saas
p
I have set up the auth for my backend to protect the routes but i also want to manage these permissions on the UI i mean if the user has a role which dont have access to create a resource i will remove the button for him i think i will need to send an API to frontend which will give all the details of the user , like list of all the permissions per resource the user has how to do this in fastest way which function shall i use
a
Hi @Pratham Khodwe, I'm an AI assistant for Permit.io. I'll analyze available sources and get back to you shortly.
To handle frontend UI permissions efficiently, you'll want to create a backend endpoint that performs bulk permission checks and returns them to your frontend. Here's the fastest approach: Create a Bulk Permission Check Endpoint The most efficient way is to use Permit's bulk checking capability through a backend API endpoint that your frontend can call (1) . Backend Implementation Create an endpoint that checks multiple permissions at once :
Copy code
const checkIfPermitted = async (user, resourcesAndActions) => {
  let isPermittedList = [];
  const promises = resourcesAndActions.map(async (resourceAndAction, index) => {
    const { resource, action, resourceAttributes } = resourceAndAction;
    const resourceObj = { type: resource, attributes: resourceAttributes };
    const permitted = await permit.check(user, action, resourceObj) ;
    console.log(permitted, "bulk") ;
    isPermittedList = [...isPermittedList, { index, permitted }];
  }) ;
  await Promise.all(promises) ;
  return isPermittedList
    .sort((a, b) => a.index - b.index)
    .map(item => item.permitted) ;
};

// add route that get user and list of resources and actions object as post parameters and check if user is permitted
<http://app.post|app.post>("/bulk", async (req, res) => {
  const { user, resourcesAndActions } = req.body;
  res.send(await checkIfPermitted(user, resourcesAndActions) ) ;
}) ;
Alternative Approach with Permit Frontend SDK For Vue.js applications, you can use the Permit Frontend SDK with a bulk loading approach (2) :
Copy code
import { PureAbility } from '@casl/ability';
import { Permit } from 'permit-fe-sdk';
import { defineStore } from 'pinia';
import { onMounted, ref, watch } from 'vue';
import { useUserStore } from './user';
export const useAbilitiesStore = defineStore('abilities', () => {
  const current = ref(new PureAbility([]) ) ;
  const user = useUserStore() ;
  const permit = ref(
    Permit({
      loggedInUser: user.current,
      backendUrl: '<http://localhost:3000/abilities>'
    })
  ) ;
  const setAbility = async () => {
    permit.value = Permit({
      loggedInUser: user.current,
      backendUrl: '<http://localhost:3000/abilities>'
    }) ;
    permit.value.reset() ;
    await permit.value.loadLocalStateBulk([
      { action: 'create', resource: 'Meal' },
      { action: 'read', resource: 'Meal' },
      { action: 'delete', resource: 'Meal' },
      { action: 'create', resource: 'Order' },
      { action: 'read', resource: 'Order' },
      { action: 'fulfill', resource: 'Order' },
      { action: 'assign-rider', resource: 'Order' },
      { action: 'deliver', resource: 'Order' }
    ]) ;
    const caslConfig = permit.value.getCaslJson() ;
    current.value = new PureAbility(
      caslConfig && caslConfig.length ? caslConfig : []
    ) ;
  };
  onMounted(async () => {
    await setAbility() ;
    watch(
      () => user.current,
      async (_) => await setAbility()
    ) ;
    document.addEventListener(
      'visibilitychange',
      async (_) => await setAbility()
    ) ;
  }) ;
  return { current, permit };
}) ;
Frontend Usage Once you have the permissions loaded, you can conditionally render UI elements based on the permissions (1) :
Copy code
<div className="bg-white p-6 rounded shadow">
  <div className="flex justify-between items-center mb-4">
    <h2 className="text-xl font-semibold">Projects</h2>
    {permissions["create:projects"] && (
      <button className="px-3 py-1 bg-blue-600 text-white text-sm rounded hover:bg-blu
Feel free to tag me with additional questions. I also appreciate feedback, it helps me improve. 👍 button 👎 button
Appreciate the feedback! Feel free to ask me again with additional guidance or context.
o
In general checkout our frontend SDK guide https://docs.permit.io/integrations/feature-flagging/casl