use*_*825 22 meteor iron-router
我在Meteor中使用内置的loginButtons选项,我想在用户登录后重定向.使用内置的网页代码段意味着我无法使用Meteor.loginwithPassword的回调,我看不到任何内部的钩子Iron-Router进行重定向.
有什么建议?
Cha*_*ris 22
Meteor经常渲染得如此之快,以至于在定义用户之前正在加载页面.您需要使用Meteor.loggingIn(),以考虑您是的情况在这个过程中,登录的此代码的工作对我来说:
this.route('myAccount', {
path: '/',
onBeforeAction: function () {
if (! Meteor.user()) {
if (!Meteor.loggingIn()) Router.go('login');
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个例子可能很有用
// 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');
});
Run Code Online (Sandbox Code Playgroud)
它应该很容易添加如下:
Tracker.autorun(function() {
var currentRoute = Router.current();
if (currentRoute === null) {
return;
}
if (currentRoute.route.getName() === 'login' && Meteor.user() !== null)
Router.go('WelcomeNewUser');
}
Run Code Online (Sandbox Code Playgroud)
如果用户未登录,您也可以将相同的路径与另一个模板一起使用.
就是这样的:
this.route('myAccount', {
before: function () {
if (!Meteor.user()) {
this.render('login');
this.stop();
}
}
}
Run Code Online (Sandbox Code Playgroud)
没有魔法,只是查看了文档 ;)