3

我正在尝试使用 AngularJS 登录到 Meteor 中的特定页面后重定向。但不知何故,它不起作用。登录 Meteor.user() 后返回 null。因此,每次它只路由到消息页面。我从其中一个论坛中看到了这个例子,并在此基础上进行了开发。

angular.module("jaarvis").run(["$rootScope", "$state", "$meteor", function($rootScope, $state, $meteor) {

   $meteor.autorun($rootScope, function(){
      if (! Meteor.user()) {
        console.log('user');
        if (Meteor.loggingIn()) {
          console.log('loggingIn ' + Meteor.user()); -- returning null
          if(Meteor.user()) {
            $state.go('onlineusers');
          } else {
            //On login
            $state.go("messages");
          }
        }
        else{
          console.log('login');
          $state.go('login');
        }
      }
     });

}]);

路由声明如下。

angular.module('jaarvis').config(['$urlRouterProvider', '$stateProvider', '$locationProvider',

    function($urlRouterProvider, $stateProvider, $locationProvider){
      $locationProvider.html5Mode(true);
      $stateProvider
        .state('login', {
          url: '/login',
          templateUrl: 'login.ng.html',
          controller: 'LoginCtrl'
        })
        .state('onlineusers',{
          url: '/onlineusers',
          templateUrl: 'client/onlineusers/onlineusers.ng.html',
          controller: 'OnlineUsersCtrl'
        })
        .state('messages', {
          url: '/messages',
          templateUrl: 'client/chats.ng.html',
          controller: 'ChatCtrl'
        })
        });
      $urlRouterProvider.otherwise("/messages");
    }]);

使用下面的代码片段进行日志记录。

<meteor-include src="loginButtons"></meteor-include>
4

2 回答 2

2

Michael 关于问题的根本原因可能是正确的,但我认为Angular-Meteor的身份验证方法提供了更好的选择。

您要做的是强制解决路由上的承诺。来自 Angular-Meteor 文档(即一般示例......):

// In route config ('ui-router' in the example, but works with 'ngRoute' the same way)
$stateProvider
  .state('home', {
    url: '/',
    templateUrl: 'client/views/home.ng.html',
    controller: 'HomeController'
    resolve: {
      "currentUser": ["$meteor", function($meteor){
        return $meteor.waitForUser();
      }]
    }
});

您的特定代码如下所示:

angular.module('jaarvis').config(['$urlRouterProvider', '$stateProvider', '$locationProvider',
function($urlRouterProvider, $stateProvider, $locationProvider){
  $locationProvider.html5Mode(true);
  $stateProvider
    .state('login', {
      url: '/login',
      templateUrl: 'login.ng.html',
      controller: 'LoginCtrl'
    })
    .state('onlineusers',{
      url: '/onlineusers',
      templateUrl: 'client/onlineusers/onlineusers.ng.html',
      controller: 'OnlineUsersCtrl',
      resolve: {
          "currentUser": ["$meteor", function($meteor){
               return $meteor.waitForUser();
           }]
      }
    })
    .state('messages', {
      url: '/messages',
      templateUrl: 'client/chats.ng.html',
      controller: 'ChatCtrl',
      resolve: {
          "currentUser": ["$meteor", function($meteor){
               return $meteor.waitForUser();
           }]
      }
    })
    });
  $urlRouterProvider.otherwise("/messages");
}]);

然后在您ChatCtrlOnlineUsersCtrl控制器上,您将添加currentUser作为要注入的变量之一,例如:

angular.module("rootModule").controller("ChatCtrl", ["$scope", "$meteor", ..., 
    function($scope, $meteor, ..., "currentUser"){
        console.log(currentUser) // SHOULD PRINT WHAT YOU WANT
    }
]);

您可能还想考虑该$meteor.requireUser()承诺,然后在承诺被拒绝时将用户送回登录页面。所有这些在 angular-meteor 网站上都有很好的记录。

祝你好运!

于 2015-09-14T03:29:37.570 回答
1

可能是用户对象尚未加载。你可以试试:

if ( Meteor.userId() ) ...

反而

于 2015-09-13T17:53:34.137 回答