1

I'm trying to build simple loggin with angular and rest api and I've got strange problem with checking whether user is logged in login form controller.

Here is my code:

everytime I refresh my app it loads current logged user

angular.module('myApp').run(['security', function(security) {
    security.requestCurrentUser();
}]);

security module is something like this (simplefied, inspired by angular-app)

angular.module('security').factory('security', ['SERVER_CONFIG', '$http', '$q', '$location', 'securityRetryQueue',
    function (SERVER_CONFIG, $http, $q, $location, queue) {

        var service = {

            currentUser : null,

            isAuthenticated : function () {
                return !!service.currentUser;
            },

            login :
                  ....    
            },

            logout : 
                  ....
            },

            requestCurrentUser : 
                  ...
            }
        };

        return service;
}]);

so it holds data about current user and using isAuthenticated you can find out wheter user is logged in or not

angular.module('security.login').controller('LoginFormCtrl', ['$scope', 'security', function ($scope, security) {

    console.log(security)
    console.log(security.currentUser)
    console.log(security.isAuthenticated())

}]);

console.log(security) returns object where property user is filled with user data so method isAuthenticated returns true

but here comes the strange thing:

security.user returns null and security.isAuthenticated() returns false and I don't understand why is user null... I need it for redirecting from loggin page when accessed and user is already logged in. I know angular app uses modal for this so it would solve my problem, but I don't want to use modal...

If anyone can explain what I'm doing wrong I would be delighted...

Thanks a lot Blažek

2
  • Is security.user the same as security.currentUser or is it another property not showing in the code? Commented May 2, 2014 at 13:24
  • Ah sorry there was a typo I did while writing into stackoverflow there should be currentUser instead of user... Edited... Commented May 2, 2014 at 15:00

1 Answer 1

0

Your application is a bit confusing in the naming of things, which may cause some conflicts. Your module is named 'security' and your factory is named 'security', this gets confusing quickly as to which one is injected where. So try renaming things just a little to something like 'securityModule' and 'securityFactory' to distinguish.

Now, the way I normally persist data through an application is with a service structured like so:

angular.module('security').service('security', ['SERVER_CONFIG', '$http', '$q', '$location', 'securityRetryQueue', function (SERVER_CONFIG, $http, $q, $location, queue) { // data that is persisted var currentUser = null; var someOtherData = null; // internal functions function getCurrentUser(){ ... do stuff ... } function doOtherStuff(){ ... do stuff ... }
// public methods // for use in controllers return { getCurrentUser : function(){ return currentUser }, setCurrentUser : function(param){ currentUser = param } isAuthenticated : function () { return !!service.currentUser; }, login : ....
}, logout : .... }, requestCurrentUser : ... } }; return service; }]);

Now another thing to look at is you "refreshing" your application, when you hit refresh, you might be (depending on browser) destroying all the stored data locally and then you'd need to fetch it from the server on first load.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply, now I see it is not clear from my answer that I do fetch user data from api on each page refresh (first code snippet) using function requestCurrentUser.... A few minutes ago I found out that there is probably problem with async comunication in requestCurrentUser, so I have to watch changes given because I accessed currentUser property before data were fetched... So I've added watcher on that property so now it works in the way I want to, anyway thank for tips

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.