0

I have a very basic login service, no real authentication, but when if i redirect with a page refresh using $window.location.href the data is lost. When the user gets 'logged in' and the page is redirected to the homepage, instead its resolves as loggedIn = false and redirects back to the login page.

How can i prevent this from happening?

Code snippets:

LoginController

ApiService.getUser($scope.user)
                .success(function (data) {
                    UserService.user = data;
                    UserService.isLogged = true;
                    $window.location.href = '/';
                })

UserService

return {
        user: {},
        isLogged: false
    };

RequireLoginService

return {
    loginRequired: function () {
        var deferred = $q.defer();

        if (!UserService.isLogged) {
            deferred.reject();
            $location.path('/login');
        } else {
            deferred.resolve()
        }

        return deferred.promise;
    }
}

app module

$routeProvider
    .when('/', {
        templateUrl: 'views/sheet.html',
        controller: 'SheetCtrl',
        resolve: {
            loginRequired: function(RequireLoginService) {
                return RequireLoginService.loginRequired();
            }
        }
    })
3
  • use $location.path() instead of window.location just like you do in RequireLoginService Commented Feb 4, 2015 at 13:53
  • That's what i did before, but i do need a refresh on the login page for i have a controller for my navbar that is outside the ng-view which requires a refresh on the whole page to show that data correctly. Commented Feb 4, 2015 at 14:00
  • 1
    When you refresh the page, your application is restarted. You have to use local storage or cookies to store information about user. Commented Feb 4, 2015 at 14:13

2 Answers 2

0

Store the user data in Local Storage and populate the input fields with that data if it's available. A useful object for storage data in Local Storage:

var LocalStorageManager = {
    setValue: function(key, value) {
        window.localStorage.setItem(key, JSON.stringify(value));
    },
    getValue: function(key) {
        try {
            return JSON.parse(window.localStorage.getItem(key));
        } catch (e) {

        }
    }
};

To store data:

LocalStorageManager.setValue("key",data);

To retrieve that data:

LocalStorageManager.getValue("key");
Sign up to request clarification or add additional context in comments.

1 Comment

how safe is this solution?
-1

Thinking in another perspective made me find an answer. I just needed to put a watch on the controller that needs the data 'refreshed' instead of trying to refresh the page as a whole.

As follows:

$scope.$watch(function () { return UserService.user.username; }, function (value) {
        $scope.user = value;
    });

Cheers.

Comments

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.