userapp-io / userapp-angular

AngularJS module that adds user authentication to your app with UserApp.
https://userapp.io
MIT License
233 stars 84 forks source link

user.hasPermission() not available in controllers. #1

Closed lrvick closed 10 years ago

lrvick commented 10 years ago

user.hasPermission() does not seem to be usable from within controllers.

For instance:

angular.module('app')
 .controller('MainCtrl', function ($scope, $location, user) {
    user.init({ appId: 'jhaw3478hgfdhu8'})
        if (user.hasPermission('permission1')){
       $location.path('/path1')
        }
        if (user.hasPermission('permission2')){
        $location.path('/path2')
        }
 });

user.hasPermission('permission1') shows false so the route never happens, but it is actually true in userapp, and in the view shows to be true.

timothyej commented 10 years ago

I'm very sorry for the late response.

This is probably because the user hasn't been loaded yet, you should run the code in the login event, like this:

$rootScope.$on('user.login', function() {
    if (user.hasPermission('permission1')){
        $location.path('/path1')
    }
    if (user.hasPermission('permission2')){
        $location.path('/path2')
    }
});

Let me know how that works out!