Meteor で組み込みの loginButtons オプションを使用していますが、ユーザーがログインした後にリダイレクトしたいと考えています。組み込みの Web スニペットを使用すると、Meteor.loginwithPassword でコールバックを使用できず、内部にフックが表示されません。リダイレクトを行うための Iron-Router。
助言がありますか?
Meteor で組み込みの loginButtons オプションを使用していますが、ユーザーがログインした後にリダイレクトしたいと考えています。組み込みの Web スニペットを使用すると、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();
}
}
}
ドキュメントを調べただけで、魔法はありません;)