我有一个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)。 –