2015-05-28 26 views
1

我有2個角色,管理員和用戶。我有一個主頁路線'/',這只是一個頁面中的登錄。當管理員用戶登錄時,他們必須轉到一條路線('adminPortal'),並且當用戶用戶登錄時,他們必須轉到'userPortal'路線。在退出這兩個角色時,請返回'/'登錄後爲不同角色管理路由的正確方法

之前,我有一個管理角色,我是在像這樣的路由的標誌:

後就正常了(其實這是打破我剛剛發現,但後來更多我的waitOn:渲染加載模板的東西)。然後我說這樣的角色(從堆棧溢出的答案,我找不到現在):

服務器/

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

而我試圖路由到像這樣的用戶/管理門戶:

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''/admin')。現在,當我第二次嘗試登錄時,除非手動將瀏覽器URL更改爲'/',否則它將始終將我帶到第一次登錄時採用的路線。所以我想我只是用Router.go('/');替換this.render('Home'),但似乎創建了某種無限循環,其中Home模板從不呈現(它現在順便第一次正確地呈現我的加載模板)。

非常感謝您閱讀所有這些!什麼是正確的方法來做到這一點?

回答

1

嘗試在註銷按鈕事件添加Router.go('/');,與Meteor.logout();

例如,沿着:

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

我有同樣的問題,因爲你,那是我發現返回的最簡單方法註銷後的主頁。

+0

哦忘了提及,我確實嘗試過,但不知道在什麼時候,它可能已經在其他一些東西之前。我無法讓它工作。你可以評論一下,如果我的'onBeforeAction'代碼btw? – Dan

+0

等待澄清一下,「註銷按鈕事件」和「Meteor.logout()」是一回事嗎?因爲我只嘗試過'Meteor.logout()',所以如果有不同的註銷按鈕事件可以嘗試? – Dan

+0

我有一個自定義註銷按鈕,調用'Router.go('chamados.todos');'(我的家模板)和'Meteor.logout();' –