登录不同角色后正确管理路线的方法 [英] Correct way to manage routes after sign in for different roles

查看:72
本文介绍了登录不同角色后正确管理路线的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个角色,管理员和用户.我有一个本地路线'/',它只是一个登录页面.管理员用户登录时,他们必须转到一条路由('adminPortal'),而用户用户登录时,他们必须转到'userPortal'路由.登出这两个角色后,应将其路由回'/'.

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();
  }
});

效果很好(实际上,它破坏了我刚刚发现的waitOn:渲染加载模板的内容,但稍后会介绍更多内容).然后,我添加了这样的角色(从我现在无法找到的Stack Overflow答案中):

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):

服务器/

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");

};

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();
  }
})

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');
  }

});

现在这几乎可以工作了!如果我以任一用户身份登录,则转到正确的门户.但是,当我注销时,我的onBeforeAction(即此问题中的第一个代码块)仅呈现Home模板,而实际上未将URL更改为'/'(即URL保持为'/user').现在,当我第二次尝试登录时,除非我手动将浏览器URL更改为'/',否则它将始终将我带到第一次登录时所采用的路由.所以我以为我只是将this.render('Home')替换为Router.go('/');,但这似乎创建了某种无限循环,其中Home模板从不渲染(它顺便说一句,这是第一次正确渲染我的加载模板)但是).

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?

推荐答案

尝试将Router.go('/');Meteor.logout();

示例:

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.

这篇关于登录不同角色后正确管理路线的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆