artgon / angularjs-role-based-auth

Role based auth example in AngularJS
162 stars 39 forks source link

startswith undefined #4

Open datachand opened 10 years ago

datachand commented 10 years ago

capture

routes.js is

'use strict';

define([
    'app/app',
    'app/controllers/home',
    'app/controllers/login',
    'app/services/authentication',
    'app/services/authorisation',
    'app/services/session'
], function(app) {

    app.run(['$rootScope', '$http' ,'$location', '$state', 'AuthenticationService', 'AuthorisationService', 'SessionService',  function ($rootScope,
        $http, $location, $state, authentication, authorisation, session) {

        var NoAuthorisationRequiredRoute = ['/login'];
        var AdminRoute = ['/admin'];

        var routeClean = function(route) {
            return _.find(NoAuthorisationRequiredRoute, function(noAuthRoute){
                return _.str.startsWith(route, noAuthRoute);
            });
        }

        var routeClean = function(route) {
            return _.find(AdminRoute, function(noAuthRoute){
                return _.str.startsWith(route, noAuthRoute);
            });
        }

        $rootScope.$on('$stateChangeStart', function(event, toState, toStateParams, toParams, fromState, fromParams){
            $rootScope.toState = toState;
            $rootScope.toStateParams = toStateParams;

            if(!routeClean($location.url()) && !authentication.isLoggedIn()) {
                event.preventDefault();
                $state.go('/login');
            } else if(AdminRoute($location.url()) && !authorisation.validateRoleAdmin(session.currentUser)) {
                event.preventDefault();
                $state.go('/error')
            }

        });

        $rootScope.$on('$stateChangeSuccess', function(currentRoute, previousRoute) {
            $rootScope.title = $state.current.title;
        });

        $rootScope.$on('$stateNotFound',
            function(event, unfoundState, fromState, fromParams){
                console.log(unfoundState.to); // "lazy.state"
                console.log(unfoundState.toParams); // {a:1, b:2}
                console.log(unfoundState.options); // {inherit:false} + default options
        });

    }]);

    app.config(['$locationProvider', '$stateProvider','$urlRouterProvider', '$httpProvider', function($locationProvider, $stateProvider, $urlRouterProvider, $httpProvider) {

        $stateProvider.state('home', {
            url: '/',
            controller: 'HomeController',
            templateUrl: '/assets/javascripts/app/partials/home.html',
            title: 'Dashboard',
            authenticate: true
        });

        $stateProvider.state('login', {
            url: '/login',
            controller: 'LoginController',
            templateUrl: '/assets/javascripts/app/partials/login.html',
            title: 'Login',
            authenticate: false
        });

        $locationProvider.html5Mode(true);

        $httpProvider.interceptors.push(function($q, $location) {
            return {
                'responseError': function(response) {
                    if(response.status === 401 || response.status === 403) {
                        $location.path('/login');
                    }
                    return $q.reject(response);
                }
            };
        });
    }]);

    return app;

});
FieldMarshallVague commented 9 years ago

It's not part of the underscore library by default, it's a plugin (underscore.string)

vikash98 commented 9 years ago

its not working. giving error ReferenceError: _ is not defined

return _.find(routesThatDontRequireAuth,