23

Meteor で組み込みの loginButtons オプションを使用していますが、ユーザーがログインした後にリダイレクトしたいと考えています。組み込みの Web スニペットを使用すると、Meteor.loginwithPassword でコールバックを使用できず、内部にフックが表示されません。リダイレクトを行うための Iron-Router。

助言がありますか?

4

4 に答える 4

22

Meteor は頻繁に非常に高速にレンダリングされるため、ユーザーが定義される前にページが読み込まれます。ログイン中Meteor.loggingIn()の状況を説明するために使用する必要があります。このコードは私にとってはうまくいきます:

this.route('myAccount', {
  path: '/',
  onBeforeAction: function () {
    if (! Meteor.user()) {
      if (!Meteor.loggingIn()) Router.go('login');
    }
  }
}
于 2014-05-06T06:28:23.233 に答える
6

この例は役に立つかもしれません

// 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');
});
于 2015-03-06T07:15:17.537 に答える
5

次のようなものを追加するだけで、非常に簡単になります。

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();
     }
   }
}

ドキュメントを調べただけで、魔法はありません;)

于 2014-04-17T21:02:33.947 に答える