简体   繁体   中英

Redirect after login AngularJS Meteor

I'm trying to redirect after login to a specific page in Meteor using AngularJS. But somehow it is not working. After login Meteor.user() is returning null. Because of this every time it is routing to messages page only. I have seen this example from one of the forums and developed on top of that.

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

}]);

Routes declared as below.

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");
    }]);

Logging using below snippet of code.

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

Michael is probably right about the root cause of the problem, but I think that a better alternative is provided by the the authentication methods of Angular-Meteor.

What you are going to want to do is to force the resolution of a promise on the route. From the Angular-Meteor docs (ie a general example...):

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

Your specific code would look something like:

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");
}]);

And then on your ChatCtrl and OnlineUsersCtrl controllers, you would add currentUser as one of the variables to inject, like:

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

You might also want to consider the $meteor.requireUser() promise as well, and then send the user back to the login page if the promise gets rejected. All of this is documented very well on the angular-meteor website.

Good luck!

It could be that the user object hasn't loaded yet. You can try:

if ( Meteor.userId() ) ...

instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM