Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directives as functions in Vue 3 #55

Closed
mreduar opened this issue Mar 22, 2022 · 4 comments
Closed

Directives as functions in Vue 3 #55

mreduar opened this issue Mar 22, 2022 · 4 comments

Comments

@mreduar
Copy link

mreduar commented Mar 22, 2022

Hi, I am trying to use the directives as functions in Vue 3, but as Vue 3 with Composition API does not have access to this.$gates then I can't find a way to make it work.

In their documentation there are examples of this, but it doesn't work in vue 3 as usual.

this.$gates.hasRole('admin'); // false
this.$gates.unlessRole('admin'); // true
this.$gates.hasAnyRole('admin|writer'); // true
this.$gates.hasAllRoles('admin|writer'); // false

I have also tried with

import { getCurrentInstance } from "vue";

const app = getCurrentInstance();
app.$gates.hasRole('admin');

But it doesn't work for me.

I am using Laravel Inertia and I implement it as follows

// resources/js/Plugins/Permissions.js
import { usePage } from '@inertiajs/inertia-vue3'

export default {
    install: (app) => {
        app.mixin({
            mounted() {
                let user = usePage().props.value.auth.user
                if (user) {
                    let authRoles = user.roleNames;
                    let authPermissions = user.permissionNames;
                    this.$gates.setRoles(authRoles);
                    this.$gates.setPermissions(authPermissions);
                }
            }
        })
    }
}
// resources/js/app.js
require('./bootstrap');

import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/inertia-vue3';
import { InertiaProgress } from '@inertiajs/progress';
import VueGates from 'vue-gates';
import Permissions from './Plugins/Permissions';
import store from './Plugins/Store';

const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => require(`./Pages/${name}.vue`),
    setup({ el, app, props, plugin }) {
        return createApp({ render: () => h(app, props) })
            .use(plugin)
            .use(VueGates) // Use package here
            .use(Permissions) // and here my implementation
            .use(store)
            .mixin({ methods: { route } })
            .mount(el);
    },
});

InertiaProgress.init({ color: '#4B5563' });

Everything works fine in the template directives, like v-role but I would like to be able to use it as functions as well.
Any ideas?

@mreduar
Copy link
Author

mreduar commented Mar 28, 2022

No suggestions?

@V35CH
Copy link

V35CH commented Aug 31, 2022

@mreduar did you found a solution?

@mreduar
Copy link
Author

mreduar commented Aug 31, 2022

@mreduar did you found a solution?

Unfortunately I did not find a solution, I am still open to a solution.
For now I have made a reusable function that checks the user roles and I can use it as a function.

const hasRole = (user, role) => {
    const roles = user.roleNames;

    return roles.some((userRole) => userRole === role);
};

export { hasRole, ... };
import { hasRole } from '@/utils/generalFunctions';

const user = usePage().props.value.auth.user;;

const isAdmin = hasRole(user, 'admin');

If someone finds a solution, do not hesitate to share it.

@thonymg
Copy link

thonymg commented Oct 18, 2022

Maybe here issues 54

@mreduar mreduar closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants