2016-11-06 53 views
0

當使用meteor,angular以及軟件包ui-router和alanning:roles開發應用程序時,我遇到了一個問題。一個組件內,我wan't一個特定的路線是隻爲記錄下某些角色的用戶可用的,所以我配置組件具有以下功能:Meteor.user()在使用角度UI路由器和alanning刷新時未定義:角色

function config($stateProvider) { 
    'ngInject'; 

    $stateProvider.state('customers', { 
     url: '/customers', 
     template: '<customer-list></customer-list>', 
     resolve: { 
      currentUser($q) { 
        const userId = Meteor.userId(); 
        if (userId && Roles.userIsInRole(userId, ['admin'])) { 
         console.log('customers.USER_REJECTED'); 
         return $q.reject('AUTH_REQUIRED'); 
        } else { 
         console.log('customers.USER_ALLOWED'); 
         return $q.resolve(); 
        } 
       } 
      } 
     }) 
    } 

它可以很好地,但是當我刷新頁面,在這種情況下,用戶遵循我實施的邏輯,即使他實際登錄,也不被允許訪問路由。如果然後我手動去/ customers,用戶仍然不被允許進入,但是如果我轉到其他唯一要求登錄的網址,允許用戶進入。我檢查過,當我刷新頁面時,Meteor.user()返回未定義的,所以我想這就是爲什麼檢查角色返回false。我已經檢查了這個社區提出的幾個解決方案,但是我不能讓他們做任何工作。你能幫我一下嗎?

非常感謝。

回答

0

只是爲了將來人們的審閱,我發現了另一種方式來做到這一點,這一個連接到角流星包:

resolve: { 
     currentUser($meteor) { 
      return $meteor.requireValidUser(function(user) { 
       if (Roles.userIsInRole(user, ['admin'])) { 
        console.log('organisations.USER_ALLOWED'); 
        return true; 
       } else { 
        console.log('organisations.USER_REJECTED'); 
        return 'AUTH_REQUIRED'; 
       } 
      }); 
     } 
    } 

我用這個功能,需要一個用戶可用並且還有一個額外的功能,您可以通過它來進行額外的驗證。重要的一點是,在角流星文檔中,他們表示這將在1.4中被棄用,所以我仍然將以前的解決方案標記爲好的解決方案。

此致敬禮,