In fact, there are a couple of related problems and ways to fix them.
Solution 1: use the function provided by Auth.isLoggedInAsync
The Auth module, which is part of the generated code, provides a method for verifying the completion of the login process (and for calling the callback function when this is the case). So, one way to fix the problem is to use this function in client code, for example:
if(form.$valid) { Auth.login({ email: $scope.user.email, password: $scope.user.password }) .then( function() { // Logged in, redirect to home console.log("Current user role: " + Auth.getCurrentUser().role); Auth.isLoggedInAsync(function(success) { console.log("Current user role: " + Auth.getCurrentUser().role); }); $location.path('/'); }) .catch( function(err) { $scope.errors.other = err.message; }); } };
In this case, you will see two operators in the console. The first will show that Auth.getCurrentUser (). The role is still undefined. The second shows that now it matters. So, if you have logic that you want to execute during login and that depends on the role of the user (or other user attributes), put that logic in the callback function that you pass to Auth.isLoggedInAsync() .
Solution 2: fix the root cause in auth.service.js
If you look at the code in client/components/auth/auth.service.js , you will see that there is a problem with asynchronous code. The problem is that currentUser = User.get(); starts an asynchronous HTTP call and that currentUser not installed immediately (this is a non-blocking call). Since the promise is immediately resolved, customers are forced to believe that the login process is complete and that all user data is available, while it is actually in flight.
In the proposed fix, the promise is resolved in the callback function passed to User.get() .
login: function (user, callback) { var cb = callback || angular.noop; var deferred = $q.defer(); $http.post('/auth/local', { email: user.email, password: user.password }). success(function (data) { $cookieStore.put('token', data.token); currentUser = User.get(function() { deferred.resolve(data); return cb(); }); }). error(function (err) { this.logout(); deferred.reject(err); return cb(err); }.bind(this)); return deferred.promise; },
Related issue and fix
The suggested code fixes one problem. Now, upon successful login, you can redirect the user to a specific page. However, a similar problem occurs after the registration procedure. In this case, also Auth.getCurrentUser().role is undefined for some time (long enough for the redirect logic to fail).
In this case, the code was fixed as follows:
createUser: function (user, callback) { var cb = callback || angular.noop; var deferred = $q.defer(); User.save(user, function (data) { $cookieStore.put('token', data.token); currentUser = User.get(function () { console.log('User.save(), user role: ' + currentUser.role); deferred.resolve(data); return cb(currentUser); }); }, function (err) { this.logout(); return cb(err); deferred.reject(err); }); return deferred.promise; },