0

i'm building a provider for the authentication part of the app. I'm having trouble on passing values to a function when using dependency injector.

angular.module('security.authorization', ['security.service'])
.provider('securityAuthorization', {
    requireRole: ['securityAuthorization', 'rolesAllowed', function(securityAuthorization, rolesAllowed) {
        return securityAuthorization.requireRole(rolesAllowed);
    }],

    $get: ['security', '$q', function(security, $q) {
        return {
            requireRole: function(rolesAllowed) {
                console.log(rolesAllowed);
            }
        };
    }]
});

I'm trying to call "requireRole" from app.config $routeProvider.

.when('/map', { controller: 'MapCtrl', templateUrl: 'partials/user/map.html',
        resolve: {
            authenticatedUser: securityAuthorizationProvider.requireRole('user')
        }
    })

But it gives me

Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: TypeError: object is not a function

UPDATE

app.config(function($routeProvider, $httpProvider, securityAuthorization) {
    $routeProvider
    .when('/map', { controller: 'MapCtrl', templateUrl: 'partials/user/map.html',
        resolve: {
            authenticatedUser: ['securityAuthorization', function(){
                return securityAuthorization.requireRole('user');
            }]
        }
    });
}); 

Error:

Error: [$injector:unpr] Unknown provider: securityAuthorization

4
  • remove securityAuthorization from your DI in your config, it is no longer needed there. Commented Oct 6, 2014 at 19:26
  • if i remove it, then i get ReferenceError: securityAuthorization is not defined Commented Oct 6, 2014 at 19:45
  • authenticatedUser: ['securityAuthorization', function(securityAuthorization){ return securityAuthorization.requireRole('user'); }] Commented Oct 6, 2014 at 20:11
  • Thanks a lot, i was struggling with the hole problem for hours. Everything is working now :) Commented Oct 6, 2014 at 20:26

1 Answer 1

1

You need to pass a function as the second parameter to provider https://docs.angularjs.org/guide/providers#provider-recipe

Since you don't appear to be using securityAuthorizationProvider, I have changed this to use a factory. If you define a Factory recipe, an empty Provider type with the $get method set to your factory function is automatically created under the hood.

.factory('securityAuthorization', ['security', '$q', function(security, $q) {
    return {
        requireRole: function(rolesAllowed) {
            // require that there is an authenticated user
            return security.auth().then(function(user) {
                if (!security.isAuthenticated()) {
                    $q.reject('/signin');
                }
                else {
                    // check if role is allowed
                    if (rolesAllowed.indexOf(user.role) == -1) {
                        $q.reject('/signin');
                    }
                    else {
                        if (user.defaultPassword) {
                            $q.reject('/change-password');
                        }
                        else {
                            // This was in the original post, but $q doesn't have resolve
                            $q.resolve();
                        }
                    }
                }
            }, function(){
                $q.reject('/signin');
            });
        }
    };
}]);

Where is how to set up the routing

app.config(function($routeProvider, $httpProvider) {
    $routeProvider
        .when('/map', { controller: 'MapCtrl', templateUrl: 'partials/user/map.html',
            resolve: {
                authenticatedUser: ['securityAuthorization', function(securityAuthorization){
                    return securityAuthorization.requireRole('user');
                }]
            }
        });
}); 
Sign up to request clarification or add additional context in comments.

7 Comments

I'm trying to call "requireRole" passing a variable like "admin" or "user" from "resolve" on "routeProvider".
In that case, you probably don't want to provider, you can just use a factory. I updated my answer to use a factory.
TypeError: undefined is not a function. When i call... .when('/map', { controller: 'MapCtrl', templateUrl: 'partials/user/map.html', resolve: {authenticatedUser: securityAuthorizationProvider.requireRole('user') } }). Answering "Do you ever use securityAuthorizationProvider in your .config(...)?" Yes! That's what i do
You want to use securityAuthorization there. I'll update my answer to show how to put it in the when.
|

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.