我正在使用 Meteor 的内置 loginButtons 选项,我想在用户登录后重定向。使用内置的网络片段意味着我不能使用 Meteor.loginwithPassword 的回调,我看不到里面的任何钩子Iron-Router 进行重定向。
有什么建议么?
我正在使用 Meteor 的内置 loginButtons 选项,我想在用户登录后重定向。使用内置的网络片段意味着我不能使用 Meteor.loginwithPassword 的回调,我看不到里面的任何钩子Iron-Router 进行重定向。
有什么建议么?
Meteor 的渲染速度通常非常快,以至于在定义用户之前就已经加载了页面。您需要使用Meteor.loggingIn()来说明您在登录过程中的情况。此代码适用于我:
this.route('myAccount', {
path: '/',
onBeforeAction: function () {
if (! Meteor.user()) {
if (!Meteor.loggingIn()) Router.go('login');
}
}
}
这个例子可能有用
// main route render a template
Router.route('/', function () {
this.render('main');
});
// render login template
Router.route('/login', function () {
this.render('login');
});
// we want to be sure that the user is logging in
// for all routes but login
Router.onBeforeAction(function () {
if (!Meteor.user() && !Meteor.loggingIn()) {
this.redirect('/login');
} else {
// required by Iron to process the route handler
this.next();
}
}, {
except: ['login']
});
// add here other routes
// catchall route
Router.route('/(.*)', function () {
this.redirect('/catchallpage');
});
只需添加以下内容应该很容易:
Tracker.autorun(function() {
var currentRoute = Router.current();
if (currentRoute === null) {
return;
}
if (currentRoute.route.getName() === 'login' && Meteor.user() !== null)
Router.go('WelcomeNewUser');
}
如果用户未登录,您也可以将相同的路由与另一个模板一起使用。
就像这样:
this.route('myAccount', {
before: function () {
if (!Meteor.user()) {
this.render('login');
this.stop();
}
}
}
没有魔法,只是查看了文档;)
您可以简单地使用您在爱尔兰路线中配置的现有路线之一
Router.go('/myRouterPathToTemplate')