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 168 forks source link

Upon login, redirect users based on registered roles #229

Closed teerachel closed 5 years ago

teerachel commented 7 years ago

Hi, this is my -client side codes-

`'submit .register-form': function(e) { e.preventDefault(); $('#myModal').modal('hide'); const email = e.target.email.value; const password = e.target.password.value; const profile = { firstName: e.target.firstName.value, lastName: e.target.lastName.value, phoneNumber: e.target.phoneNumber.value, role: e.target.role.value };

    Accounts.createUser({
        email,
        password,
        profile
    }, function (error) {
        console.log(email, password, profile, error);
        if (error) {
            console.log(error);
        } else {
            Bert.alert({
                type: 'success',
                style: 'growl-top-right',
                message: 'You are registered!',
                icons: 'fa-info'
            });
        }
        Router.go('/');
    });
}`

and server side -accounts.js-

`Meteor.startup(() => { // if users database is empty, seed these values if (Meteor.users.find().count() < 1) { // users array const users = [ { firstName: 'Bobby', lastName: 'brown', email: 'bobby@hotmail.com', password: 'brownie', roles: ['admin'] } ]; //user creation _.each(users, d => { const userId = Accounts.createUser({ email: d.email, password: d.password, profile: { firstName: d.firstName, lastName: d.lastName, phoneNumber: d.phoneNumber, }, isDefault: true, //Add this field to notify the onCreateUser callback that this is default roles: d.roles(['customer', 'driver']) }); Meteor.users.update({_id: userId}, {$set: {'emails.0.verified': true}}); Roles.addUsersToRoles(userId, d.roles); }); } Accounts.config({ loginExpirationInDays: null });

Accounts.urls.resetPassword = function (token) {
    return Meteor.absoluteUrl('ResetPassword/' + token);
};

});

`

I am trying to create users for roles customers and drivers but it doesn't set the roles at all and it shows undefined too when user is registered. Please help me out!

Thank you :)

ndrewr commented 7 years ago

I'm working on this behaviour, too.

Here is a good discussion on that topic.

If you search Issues, you can find other discussions around this.

Basically, assuming you are using Accounts package, set some redirect code to run within the onLogin callback.

Here is an example if you are using FlowRouter.

But you will also need to check for the loading status of the Roles data. I am looking at this:

Roles.subscription.ready()

as the indicator that its safe to check on the User's role.

Where you perform that check is a major point of discussion; I am putting it at the component/template level and then either showing some "loading" thing or the content once the Role sub is ready.