Correct way to manage routes after sign in for dif

2019-08-13 02:11发布

问题:

I have 2 roles, admins and users. I have a home route '/' that is just a sign in page. When admin users sign in they must go to one route ('adminPortal') and when user users log in they must go to the 'userPortal' route. On signing out both roles should route back to '/'.

Before I had an admin role, I was routing on sign in like so:

Router.onBeforeAction(function() {
  this.render('loading');
  if (! Meteor.userId()) {
    this.render('Home');
  } else {
    this.next();
  }
});

which worked fine (actually it was breaking my waitOn: render loading template stuff which I just discovered but more on that later). I then added roles like this (from a Stack Overflow answer I can't find right now):

server/

insertUsers=function(){
  var adminId=Accounts.createUser({
    username:"admin",
    password:"password"
  });

  Roles.addUsersToRoles(adminId,"admin");

  var userIdd = Accounts.createUser({
    username:"user",
    password:"password"
  });

  Roles.addUsersToRoles(userIdd,"user");

};

and

Meteor.startup(function () {
  // always start from scratch (you will want to comment this line at some point !)
  Meteor.users.remove({});
  if(Meteor.users.find().count()===0){
    insertUsers();
  }
})

and

Meteor.publish("user", function () {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId},
                             {fields: {'roles': 1}});
  } else {
    this.ready();
  }
});

And I tried to route to the user/admin portals like this:

router.js

Router.route('/', {

  before: function() {
    if (! Meteor.userId()) {  // I acutally added this check that the user is not logged in after the infinite loading problem but I thought the question was getting too long so I just left it in rather
      this.render('Home')
    } else {
      if (Roles.userIsInRole(Meteor.user(), 'admin')) {
        Router.go('/admin')
      } else {
        Router.go('/user')
      }
    }
  },

  waitOn: function () {
    return Meteor.subscribe('user');
  }

});

Now this very almost works! If I log is as either user I go to the right portal. However, when I sign out, my onBeforeAction (i.e. first code block in this question) only renders the Home template and does not actually change the URL to '/' (i.e. the URL remains either '/user' or '/admin'). Now when I try log in a second time, it will always take me to the route that I was taken to on the first log in unless I manually change the browser URL to '/'. So I thought I'd just replace the this.render('Home') with a Router.go('/'); but that seems to have created some sort of infinite loop where the Home template never renders (it did incidentally now for the first time correctly render my loading template though).

So thanks for reading all that! What's the right way to do this?

回答1:

Try adding Router.go('/'); in your logout button event, along with Meteor.logout();

Example:

Template.loginButtons.events({
    'click #login-buttons-logout' : function (event, template) {
        Meteor.logout(function(err) {
            Router.go('/');
        });
    }
});

I had the same issue as you, and that was the simplest way I've found to return to home page after logout.