All files / js/composables useAuthAbilities.ts

85.71% Statements 6/7
66.66% Branches 4/6
100% Functions 3/3
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                4x     4x   4x         4x   4x 4x                
import { usePage } from '@inertiajs/vue3';
import { computed, type ComputedRef } from 'vue';
 
function readAbility(
    abilities: Record<string, Record<string, boolean>> | null | undefined,
    module: string,
    ability: string,
): boolean {
    Iif (!abilities) {
        return false;
    }
    const mod = abilities[module];
 
    return !!(mod && mod[ability]);
}
 
/** Reactive check against shared Inertia `auth.abilities` (same source as the sidebar). */
export function useAuthAbility(module: string, ability: string): ComputedRef<boolean> {
    const page = usePage();
 
    return computed(() =>
        readAbility(
            (page.props.auth as { abilities?: Record<string, Record<string, boolean>> | null } | undefined)?.abilities ??
                null,
            module,
            ability,
        ),
    );
}