<commit_before>angular.module('engage', ['ui.router', 'ngMaterial'])

///////////////////// Setup & Config /////////////////////

.config(function($stateProvider, $urlRouterProvider, $httpProvider){

    // Interceptor to redirect to /login upon errors due to no session
    $httpProvider.interceptors.push(function($q, $injector, LocalStorage){

        return {
            'responseError': function(rejection){

                // If we get a 403 error, we're assuming that it means there is no session
                if (rejection.status === 403){
                    $injector.invoke(function($location){
                        $location.path('/login');
                    });
                }

                return rejection;
            }
       }; 
    });
    $stateProvider
        .state('login', {
            url: '/login',
            templateUrl: 'partials/login.html',
            controller: 'LoginCtrl'
        })
        .state('nav', { // Just a container with the nav bar & headers
            url: '',
            templateUrl: 'partials/nav.html',
            controller: 'NavCtrl'
        })
        .state('nav.home', { // The actual default homepage
            url: '/home',
            templateUrl: 'partials/home.html',
            controller: 'HomeCtrl'
        })
        .state('nav.addwords', {
            url: '^/addwords',
            templateUrl: 'partials/add-words.html',
            controller: 'AddWordsCtrl'
        })
        .state('nav.idea', {
            url: '^/ideas/:id',
            templateUrl: 'partials/idea.html',
            controller: 'IdeaCtrl'
        })
        .state('nav.profile', {
            url: '^/profile',
            templateUrl: 'partials/profile.html',
            controller: 'ProfileCtrl'
        })
        .state('nav.profile.user', {
            url: '/:id',
            templateUrl: 'partials/profile.html',
            controller: 'ProfileCtrl'
        });

    $urlRouterProvider.otherwise('/home');
})
.run(function($rootScope, $location, LocalStorage, AuthService){
    // Listener that checks on each state change whether or not we have an auth token. If we don't, redirect to /login.
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){ 
        var isLoggedIn = AuthService.validate();

        if (!isLoggedIn) {
            $location.path('/login');
            return;
        }

        // If trying to go to /login (while logged in already), 
        // or coming from /login (just finished AuthService.oAuthAuthorize), 
        // or navigating to the base URL (awesome.com/)
        // go /home
        if (toState.name === 'login' || fromState.name === 'login' || !toState.url) {
            $location.path('/home');
        }
    });
})

///////////////////// Factories & Services /////////////////////

.factory('LocalStorage', function($window){
    var store = $window.localStorage;

    return service = {
        set: function(key, value){
           return store.setItem(key, value);
        },
        get: function(key){
            return store.getItem(key);
        },
        remove: function(key){
            return store.removeItem(key);
        },
        clear: function(){
            return store.clear();
        }
    };
})
.factory('AuthService', function($rootScope, $q, $window, $http, $state, $location, LocalStorage){

    function parseResponse(oAuthObj) {
        for (var key in oAuthObj) {
            var value = oAuthObj[key];
            oAuthObj[key] = decodeURIComponent(value);
        }
        return oAuthObj;
    }

    return auth = {
        simpleLogin: function(username, password){

            var loginConfig = {
                method: 'POST',
                url: '/api/login',
                data: {
                    username: username,
                    password: password
                }
            };

            return $http(loginConfig)
                .then(function(response){
                    LocalStorage.set('userinfo', JSON.stringify( response.data ) );
                    console.log('Signed in: ', response.data);
                    // $state.go('nav.home');
                    $location.path('/home');
                    return response.data;<commit_msg>Properly reject 403 errors, clear Localstorage (via .logout method) on no-session<commit_after>angular.module('engage', ['ui.router', 'ngMaterial'])

///////////////////// Setup & Config /////////////////////

.config(function($stateProvider, $urlRouterProvider, $httpProvider){

    // Interceptor to redirect to /login upon errors due to no session
    $httpProvider.interceptors.push(function($q, $injector, LocalStorage){

        return {
            'responseError': function(rejection){

                // If we get a 403 error, we're assuming that it means there is no session
                if (rejection.status === 403){
                    $injector.invoke(function($location){
                        $location.path('/login');
                    });
                }

                return $q.reject(rejection);
            }
       }; 
    });
    $stateProvider
        .state('login', {
            url: '/login',
            templateUrl: 'partials/login.html',
            controller: 'LoginCtrl'
        })
        .state('nav', { // Just a container with the nav bar & headers
            url: '',
            templateUrl: 'partials/nav.html',
            controller: 'NavCtrl'
        })
        .state('nav.home', { // The actual default homepage
            url: '/home',
            templateUrl: 'partials/home.html',
            controller: 'HomeCtrl'
        })
        .state('nav.addwords', {
            url: '^/addwords',
            templateUrl: 'partials/add-words.html',
            controller: 'AddWordsCtrl'
        })
        .state('nav.idea', {
            url: '^/ideas/:id',
            templateUrl: 'partials/idea.html',
            controller: 'IdeaCtrl'
        })
        .state('nav.profile', {
            url: '^/profile',
            templateUrl: 'partials/profile.html',
            controller: 'ProfileCtrl'
        })
        .state('nav.profile.user', {
            url: '/:id',
            templateUrl: 'partials/profile.html',
            controller: 'ProfileCtrl'
        });

    $urlRouterProvider.otherwise('/home');
})
.run(function($rootScope, $location, LocalStorage, AuthService){
    // Listener that checks on each state change whether or not we have an auth token. If we don't, redirect to /login.
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){ 
        var isLoggedIn = AuthService.validate();

        if (!isLoggedIn) {
            AuthService.logout();
            return;
        }

        // If trying to go to /login (while logged in already), 
        // or coming from /login (just finished AuthService.oAuthAuthorize), 
        // or navigating to the base URL (awesome.com/)
        // go /home
        if (toState.name === 'login' || fromState.name === 'login' || !toState.url) {
            $location.path('/home');
        }
    });
})

///////////////////// Factories & Services /////////////////////

.factory('LocalStorage', function($window){
    var store = $window.localStorage;

    return service = {
        set: function(key, value){
           return store.setItem(key, value);
        },
        get: function(key){
            return store.getItem(key);
        },
        remove: function(key){
            return store.removeItem(key);
        },
        clear: function(){
            return store.clear();
        }
    };
})
.factory('AuthService', function($rootScope, $q, $window, $http, $state, $location, LocalStorage){

    function parseResponse(oAuthObj) {
        for (var key in oAuthObj) {
            var value = oAuthObj[key];
            oAuthObj[key] = decodeURIComponent(value);
        }
        return oAuthObj;
    }

    return auth = {
        simpleLogin: function(username, password){

            var loginConfig = {
                method: 'POST',
                url: '/api/login',
                data: {
                    username: username,
                    password: password
                }
            };

            return $http(loginConfig)
                .then(function(response){
                    LocalStorage.set('userinfo', JSON.stringify( response.data ) );
                    console.log('Signed in: ', response.data);
                    $location.path('/home');
                    return response.data;
{'fraction_non_alphanumeric': 0.14747992084739844, 'fraction_numerical': 0.0017460132697008498, 'mean_word_length': 1.5965548504079783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1151966', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2182, 'n_words': 524}