<commit_before>'use strict';

// Module with AngularJS services and directives that integrates UserApp into your app
// https://github.com/userapp-io/userapp-angular
var userappModule = angular.module('UserApp', []);

(function(){
    // Expose the UserApp API
    userappModule.value('UserApp', UserApp);

    // Directive error handler
    var handleError = function(scope, error, elementId) {
        if (!error) {
            return;
        }
        
        error.handled = false;

        if (elementId) {
            error.handled = true;
            angular.element(document.getElementById(elementId)).text(error.message);
        }

        scope.$emit('user.error', error);
    };

    // Function to detect if a route is public or not
    var isPublic = function(route) {
        return (route.public == true || route.login == true || route.verify_email == true || route.set_password == true);
    };

    // Authentication service
    userappModule.factory('user', function($rootScope, $location, $injector, $log, $timeout, $q) {
        var user = {};
        var appId = null;
        var options = null;
        var token = Cookies.get('ua_session_token');
        var status = { authorized: false, authenticated: false };
        var heartBeatInterval = -1;
        var defaultRoute = null;
        var loginRoute = null;
        var states = {};

        // Check if either ng-route or ui-router is present
        if ($injector.has) {
            var $route = $injector.has('$route') ? $injector.get('$route') : null;
            var $state = $injector.has('$state') ? $injector.get('$state') : null;
        } else {
            var $route = $injector.get('$route');
            var $state = null;
        }

        if ($state && (!$state.transitionTo || !$state.get)) {
            // This is not the correct $state service
            $state = null;
        }
        
        if (!$state && !$route) {
            $log.warn('The UserApp module needs either ng-route or ui-router to work as expected.');
        }

        if ($state) {
            // Get the list of all states
            var stateList = $state.get();
            for (var i = 0; i < stateList.length; ++i) {
                states[stateList[i].name] = stateList[i];
            }
        }

        var transitionTo = function(state, useLocation) {
            if ($state) {
                if (useLocation) {
                    $location.path(states[state].url);
                } else {
                    $state.transitionTo(state);
                }
            } else if ($route) {
                $location.path(state);
            }
        };

        // Controller for the verify email route
        var verifyEmailController = function($scope, $location, $timeout) {
            $scope.loading = true;
            
            var emailToken = $location.search().email_token;
            service.verifyEmail(emailToken, function(error, result) {
                if (error) {
                    $timeout(function() {
                        $scope.error = error;
                    });
                } else {
                    $timeout(function() {
                        $scope.loading = false;
                    });
                }
            });
        };

        var authenticationRequiredHandler = function() {
            $timeout(function() {
                transitionTo(loginRoute);
            });
        };


        var accessDeniedHandler = function() {
            $timeout(function () {
                transitionTo(defaultRoute, true);
            });
        };

        /**
         * Invokes authenticationRequired/accessDeniedHandler if state is protected. Prevents state transition
         * from happening in these cases if a stateChangeStartEvent is given.
         */
        var checkAccessToState = function(state, stateParams, stateChangeStartEvent) {
            if ((!state.data || (state.data && isPublic(state.data) == false)) && status.authenticated == false) {
                if(stateChangeStartEvent)
                    stateChangeStartEvent.preventDefault();
                authenticationRequiredHandler(state, stateParams);
            } else if (state.data && state.data.hasPermission && user.permissions) {
                if (!service.hasPermission(state.data.hasPermission)) {
                    if(stateChangeStartEvent)
                        stateChangeStartEvent.preventDefault();
                    accessDeniedHandler(user, state, stateParams);
                }
            } else if (state.data && state.data.authCheck && status.authenticated) {
                if (!state.data.authCheck(user, stateParams)) {
                    if(stateChangeStartEvent)
                        stateChangeStartEvent.preventDefault();
                    accessDeniedHandler(user, state, stateParams);
                }
            }
        };

        /**
         * Invokes authenticationRequired/accessDeniedHandler if route is protected. Prevents route transition
         * from happening in these cases if a routeChangeStartEvent is given.
         */
        var checkAccessToRoute = function(routeData, routeChangeStartEvent) {
            if (routeData.$$route && isPublic(routeData.$$route) == false && status.authenticated == false) {
                if(routeChangeStartEvent)
                    routeChangeStartEvent.preventDefault();<commit_msg>only perform auth check when user is loaded<commit_after>'use strict';

// Module with AngularJS services and directives that integrates UserApp into your app
// https://github.com/userapp-io/userapp-angular
var userappModule = angular.module('UserApp', []);

(function(){
    // Expose the UserApp API
    userappModule.value('UserApp', UserApp);

    // Directive error handler
    var handleError = function(scope, error, elementId) {
        if (!error) {
            return;
        }
        
        error.handled = false;

        if (elementId) {
            error.handled = true;
            angular.element(document.getElementById(elementId)).text(error.message);
        }

        scope.$emit('user.error', error);
    };

    // Function to detect if a route is public or not
    var isPublic = function(route) {
        return (route.public == true || route.login == true || route.verify_email == true || route.set_password == true);
    };

    // Authentication service
    userappModule.factory('user', function($rootScope, $location, $injector, $log, $timeout, $q) {
        var user = {};
        var appId = null;
        var options = null;
        var token = Cookies.get('ua_session_token');
        var status = { authorized: false, authenticated: false };
        var heartBeatInterval = -1;
        var defaultRoute = null;
        var loginRoute = null;
        var states = {};

        // Check if either ng-route or ui-router is present
        if ($injector.has) {
            var $route = $injector.has('$route') ? $injector.get('$route') : null;
            var $state = $injector.has('$state') ? $injector.get('$state') : null;
        } else {
            var $route = $injector.get('$route');
            var $state = null;
        }

        if ($state && (!$state.transitionTo || !$state.get)) {
            // This is not the correct $state service
            $state = null;
        }
        
        if (!$state && !$route) {
            $log.warn('The UserApp module needs either ng-route or ui-router to work as expected.');
        }

        if ($state) {
            // Get the list of all states
            var stateList = $state.get();
            for (var i = 0; i < stateList.length; ++i) {
                states[stateList[i].name] = stateList[i];
            }
        }

        var transitionTo = function(state, useLocation) {
            if ($state) {
                if (useLocation) {
                    $location.path(states[state].url);
                } else {
                    $state.transitionTo(state);
                }
            } else if ($route) {
                $location.path(state);
            }
        };

        // Controller for the verify email route
        var verifyEmailController = function($scope, $location, $timeout) {
            $scope.loading = true;
            
            var emailToken = $location.search().email_token;
            service.verifyEmail(emailToken, function(error, result) {
                if (error) {
                    $timeout(function() {
                        $scope.error = error;
                    });
                } else {
                    $timeout(function() {
                        $scope.loading = false;
                    });
                }
            });
        };

        var authenticationRequiredHandler = function() {
            $timeout(function() {
                transitionTo(loginRoute);
            });
        };


        var accessDeniedHandler = function() {
            $timeout(function () {
                transitionTo(defaultRoute, true);
            });
        };

        /**
         * Invokes authenticationRequired/accessDeniedHandler if state is protected. Prevents state transition
         * from happening in these cases if a stateChangeStartEvent is given.
         */
        var checkAccessToState = function(state, stateParams, stateChangeStartEvent) {
            if ((!state.data || (state.data && isPublic(state.data) == false)) && status.authenticated == false) {
                if(stateChangeStartEvent)
                    stateChangeStartEvent.preventDefault();
                authenticationRequiredHandler(state, stateParams);
            } else if (state.data && state.data.hasPermission && user.permissions) {
                if (!service.hasPermission(state.data.hasPermission)) {
                    if(stateChangeStartEvent)
                        stateChangeStartEvent.preventDefault();
                    accessDeniedHandler(user, state, stateParams);
                }
            }
            // only do auth check if user is loaded (indicated by user_id present)
            else if (state.data && state.data.authCheck && user.user_id) {
                if (!state.data.authCheck(user, stateParams)) {
                    if(stateChangeStartEvent)
                        stateChangeStartEvent.preventDefault();
                    accessDeniedHandler(user, state, stateParams);
                }
            }
        };

        /**
         * Invokes authenticationRequired/accessDeniedHandler if route is protected. Prevents route transition
         * from happening in these cases if a routeChangeStartEvent is given.
         */
        var checkAccessToRoute = function(routeData, routeChangeStartEvent) {
            if (routeData.$$route && isPublic(routeData.$$route) == false && status.authenticated == false) {
                if(routeChangeStartEvent)
                    routeChangeStartEvent.preventDefault();
{'fraction_non_alphanumeric': 0.11152687779501688, 'fraction_numerical': 0.0003650634297709227, 'mean_word_length': 1.6983501600590987, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '72145', 'n_tokens_mistral': 2768, 'n_tokens_neox': 2659, 'n_words': 666}