2015-07-19 33 views
1

我使用onBeforeActions檢查用戶是否登錄,如果他們的配置文件不完整或被禁用。這似乎工作正常。鐵路由器onBeforeActions重定向到起始頁

但現在的問題是,每當我直接進入應用程序中的頁面,我也會重定向到startPage。我調試並發現'用戶'是未定義的,雖然我登錄了。我使用帳戶-fb & -tw和account-ui軟件包。

如何確保用戶已登錄?我不明白的功能的正確時間。

編輯: 本地我似乎並不總是有這個問題。用戶經常被定義。但我的生產服務器上(我只是一個調試器語句上傳它,是我沒有TESTSERVER:/),它似乎總是不定,轉發我的起始頁..

router.js:

if (Meteor.isClient) { 
// only for client, else the serverside router will try to run this onBeforeAction 
var onBeforeActions; 

onBeforeActions = { 
    loginRequired: function() { 
     var user = Meteor.user(); 
     if (!user) { 
      Router.go('startPage'); 
     } else { 
      if (user && user.profile && user.profile.incomplete) { 
       Router.go('completeSignup'); 
      } 

      if (user && user.profile && user.profile.disable) { 
       Router.go('profileEdit'); 
      } 
     } 

     this.next(); 
    } 
}; 

Router.onBeforeAction(onBeforeActions.loginRequired); 

Router.configure({ 
    notFoundTemplate: 'notFound', 
    layoutTemplate: 'layoutMain', 
    loadingTemplate: 'loading', 
    trackPageView: true 
}); 

Router.map ({}); 

} 

回答

1

用戶是否正在登錄? Meteor.loggingIn()返回什麼?這是我在Coffeescript中使用Iron Router的登錄檢查。

requireLogin = -> 
    if not Meteor.user() 
    if Meteor.loggingIn() 
     this.render this.loadingTemplate 
    else 
     this.render 'accessDenied' 
    else 
    this.next() 

嘗試一下這樣簡單的事情,然後添加你想要做的事情的一點點。但是我相當確定Meteor.loggingIn()就是你在這裏尋找的東西。

這裏是它的文檔:http://docs.meteor.com/#/full/meteor_loggingin

編輯:這可能是本地工作,因爲沒有任何延遲,所以,如果你已經登錄它可以立即運行一切。如果這是有道理的。

+0

哇,太簡單了!謝謝@DustinRiley! – flowen

相關問題