0
votes

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

1
remove securityAuthorization from your DI in your config, it is no longer needed there. - TheSharpieOne
if i remove it, then i get ReferenceError: securityAuthorization is not defined - Guilherme Miranda
authenticatedUser: ['securityAuthorization', function(securityAuthorization){ return securityAuthorization.requireRole('user'); }] - TheSharpieOne
Thanks a lot, i was struggling with the hole problem for hours. Everything is working now :) - Guilherme Miranda

1 Answers

1
votes

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');
                }]
            }
        });
});