Meteor-Community-Packages / meteor-roles

Authorization package for Meteor, compatible with built-in accounts packages
http://meteor-community-packages.github.io/meteor-roles/
MIT License
921 stars 167 forks source link

Problem with `Vue Router` #245

Closed thearabbit closed 4 years ago

thearabbit commented 7 years ago
router. beforeEach((to, from, next) => {
            // Check user
            if (!Meteor.loggingIn() && !Meteor.userId()) {
                next({path: '/login'});
            } else {
                // Check role
                    let loggedInUser = Meteor.userId();
                    let role = Roles.userIsInRole(loggedInUser, ['posts.insert'], 'Entry');

                    if (role) {
                        next();
                    }else{
                       alert('no role'); 
                   }
            }
    });

every time show false;

mitar commented 7 years ago

Vue router does not support tracker reactivity. Yet. See work in progress here: https://github.com/mitar/vue-test

thearabbit commented 7 years ago

Thanks for your reply. Could we solve or any solution for this?

mitar commented 7 years ago

Yes, it is solvable. But the solution yet has to be made and tested.

mitar commented 7 years ago

BTW, I am not sure that beforeEach is even reactive even inside Vue itself.

thearabbit commented 7 years ago

Excuse me, what about vue-meteor-tracker of Akryum?

thearabbit commented 7 years ago

in Akryum issue But I don't understand

image

thearabbit commented 7 years ago

Now I tries

// Method
export const userIsInRole = new ValidatedMethod({
    name: 'userIsInRole',
    mixins: [CallPromiseMixin],
    validate: new SimpleSchema({
        role: {type: String},
    }).validator(),
    run({role}) {
        if (!this.isSimulation) {
            return Roles.userIsInRole(Meteor.userId(), role);
        }
    }
});

---------
// router
router.beforeResolve((to, from, next) => {
            // Check user
            if (!Meteor.loggingIn() && !Meteor.userId()) {
                next({path: '/login'});
            } else {
                userIsInRole.callPromise({role: to.name}).then((result) => {
                    if (result) {
                        next();
                    } else {
                        next(false);
                        alert('no roles');
                    }
                }).catch((err) => {
                    console.log(err.reason);
                });
            }
    });

It work fine, please advice...