我有一個Meteor應用程序,我希望用戶在登錄後和登出後轉到儀表板(稱爲documentsIndex)頁面,將它們重定向到Web應用程序的首頁。現在,我有以下幾點:流星在登錄和註銷後重定向
Iron.Router.hooks.requireLogin = function() {
if (! Meteor.user()) {
if (Meteor.loggingIn()) {
this.render('this.loadingTemplate');
} else {
this.render('accessDenied');
}
} else {
this.next();
}
};
Iron.Router.hooks.goToDashboard = function() {
if (Meteor.user()) {
Router.go('documentsIndex');
this.next();
} else {
this.next();
}
};
Iron.Router.hooks.goToFrontpage= function() {
if (!Meteor.user()) {
Router.go('frontpage');
this.next();
} else {
this.next();
}
};
Router.onBeforeAction('goToDashboard', {except: ['documentNew', 'documentIndex', 'documentShow', 'documentEdit']});
Router.onBeforeAction('goToFrontpage', {except: ['frontpage', 'about']});
Router.onBeforeAction('requireLogin', {except: ['frontpage', 'about']});
Router.onBeforeAction('dataNotFound', {only: ['documentIndex','documentNew', 'documentIndex', 'documentShow', 'documentEdit']});
這工作,所以當用戶登錄時,他總是重定向到DocumentsIndex路線,他可以導航的後端。當用戶退出時,他被重定向到首頁並可以瀏覽前端。
- 當我的webapp將來會增長時(特別是onBeforeAction中唯一的和除了語句會變得容易混淆和容易出錯),這會變得很難管理。所以理想情況下,我想將所有內容結合在requireLogin鉤子中。
- 稍後我將與角色(alanning:roles)一起工作。我將擁有「管理員」角色,已註冊的「客戶」角色,然後是隻能訪問首頁的普通訪問者。在Router鉤子中使用這個角色的任何想法?一個例子將不勝感激。
注意:在使用IM帳號密碼包
在登錄後轉到特定路由很好,但當用戶嘗試訪問需要身份驗證的深層鏈接時,導致用戶體驗差。然後你強迫他們登錄,但不要把他們帶到深層次的鏈接。我有一個[例子router.js文件](http://0rocketscience.blogspot.com/2015/08/meteor-build-clean-easy-to-maintain.html),涵蓋了這一點。根據@ challett的回答,這與'Accounts.onLogin'和'Meteor.logout'鉤子相結合,總是把用戶帶到*期待的地方。 –
非常感謝您的鏈接。我非常感興趣地閱讀它。你怎麼能把這些角色用到你的router.js文件中? – wiwa1978
那裏已經有2個角色了(正常和管理員),但我還沒有納入[alanning:roles](https://atmospherejs.com/alanning/roles)。 –