<commit_before>// set up global dependencies
ace.config.set("basePath", "assets/bower/ace-builds/src-min-noconflict/");
// CKEDITOR.basePath = 'assets/ckeditor/';
CKEDITOR.plugins.basePath = 'assets/ckeditor/plugins/';

// declare the module
angular.module('gandhi')

.config(function($stateProvider, $urlRouterProvider, RestangularProvider) {
	$urlRouterProvider.otherwise("/");

	RestangularProvider.setBaseUrl('api');
	RestangularProvider.addFullRequestInterceptor(function(element, operation, route, url, headers, params, httpConfig) {
		return {
			element: element,
			params: params,
			headers: headers, // TODO: put JWT here
			httpConfig: httpConfig
		};
	});

})

.run(function($rootScope, Restangular, $window, $state, $stateParams) {
	$rootScope.$state = $state;
	$rootScope.$stateParams = $stateParams;

	function setUser(token){
		$rootScope.currentUser = null;

		if(token)
			$window.localStorage.token = token;

		if(typeof $window.localStorage.token == 'undefined')
			return;

		var output = $window.localStorage.token.split('.')[1].replace('-', '+').replace('_', '/');
		while (output.length % 4 > 0){ output += "=" };

		var parsed = JSON.parse(window.atob(output));

		Restangular.one("users", parsed.sub).get().then(function(user){
			$rootScope.currentUser = user;
		}, function(err){
			console.log(err);
			$rootScope.logout();
		})
	}

	// set the user
	setUser();

	$rootScope.login = {
		email: '',
		password: '',
		submit: function(){
			Restangular.service('tokens').post({email: this.email, password: <PASSWORD>})
				.then(function(data){
					setUser(data.token);
					$rootScope.login.reset();
				}, function(err){
					$rootScope.login.reset();
					if(err.data && err.data.message)
						alert(err.data.message);
					else
						alert('Unable to log in. Please try again.');
				})
		},
		reset: function(){
			this.password = '';
		}
	}

	$rootScope.signup = {
		name: '',
		email: '',
		password: '',
		password2: '',
		submit: function(){
			if(this.password != this.password2)
				return alert('Passwords must match.');

			Restangular.service('users').post({name: this.name, email: this.email, password: this.password})
				.then(function(data){
					$rootScope.login.email = $rootScope.signup.email;
					$rootScope.login.password = <PASSWORD>;
					$rootScope.login.submit();
					$rootScope.signup.reset();
				}, function(err){
					if(err.data && err.data.message)
						alert(err.data.message);
					else
						alert('Unable to sign up. Please try again.');
				})
		},
		reset: function(){
			this.name = this.email = this.password = <PASSWORD> = '';
		}
	}

	$rootScope.logout = function(){
		delete localStorage.token;
		$rootScope.currentUser = null;
	}

})
<commit_msg>Make auth service<commit_after>// set up global dependencies
ace.config.set("basePath", "assets/bower/ace-builds/src-min-noconflict/");
// CKEDITOR.basePath = 'assets/ckeditor/';
CKEDITOR.plugins.basePath = 'assets/ckeditor/plugins/';

// declare the module
angular.module('gandhi')

.config(function($stateProvider, $urlRouterProvider, RestangularProvider) {
	$urlRouterProvider.otherwise("/");

	RestangularProvider.setBaseUrl('api');
	RestangularProvider.addFullRequestInterceptor(function(element, operation, route, url, headers, params, httpConfig) {
		return {
			element: element,
			params: params,
			headers: headers, // TODO: put JWT here
			httpConfig: httpConfig
		};
	});

})

.factory('auth', function($rootScope, Restangular, $window, $q){

	function logout() {
		delete localStorage.token;
		$rootScope.currentUser = null;
	}

	function setUser(token) {
		$rootScope.currentUser = null;

		if(token)
			$window.localStorage.token = token;

		if(typeof $window.localStorage.token == 'undefined')
			return $q.when(true);

		var output = $window.localStorage.token.split('.')[1].replace('-', '+').replace('_', '/');
		while (output.length % 4 > 0){ output += "=" };

		var parsed = JSON.parse(window.atob(output));

		return Restangular.one("users", parsed.sub).get().then(function(user){
			return $rootScope.currentUser = user;
		}, function(err){
			logout();
			return $q.reject(err);
		});
	}

	function login(data) {
		if(!data || !data.email)
			return $q.reject(new Error('Email is required'));

		return Restangular.service('tokens').post(data).then(function(result){
			return setUser(result.token);
		});
	}

	function signup(data){
		if(!data || !data.email)
			return $q.reject(new Error('Email is required'));

		if(!data.password)
			return $q.reject(new Error('Password is required'));

		return Restangular.service('users').post(data).then(login, $q.reject);
	}

	// set the user
	setUser();

	return {
		login: login,
		signup: signup,
		logout: logout
	}
})

.run(function($rootScope, auth, $state, $stateParams) {
	$rootScope.$state = $state;
	$rootScope.$stateParams = $stateParams;

	$rootScope.login = {
		email: '',
		password: '',
		submit: function(){
			auth.login({email: this.email, password: this.password})
				.then(function(data){
					$rootScope.login.reset();
				}, function(err){
					$rootScope.login.reset();
					if(err.data && err.data.message)
						alert(err.data.message);
					else
						alert('Unable to log in. Please try again.');
				})
		},
		reset: function(){
			this.password = '';
		}
	}

	$rootScope.signup = {
		name: '',
		email: '',
		password: '',
		password2: '',
		submit: function(){
			if(this.password != this.password2)
				return alert('Passwords must match.');

			auth.signup({name: this.name, email: this.email, password: this.password})
				.then(function(data){
					$rootScope.signup.reset();
				}, function(err){
					if(err.data && err.data.message)
						alert(err.data.message);
					else
						alert('Unable to sign up. Please try again.');
				})
		},
		reset: function(){
			this.name = this.email = this.password = this.password2 = '';
		}
	}

	$rootScope.logout = auth.logout;

})

{'fraction_non_alphanumeric': 0.17618160257496188, 'fraction_numerical': 0.0018634592580044046, 'mean_word_length': 3.792207792207792, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '7439120', 'n_tokens_mistral': 2252, 'n_tokens_neox': 1935, 'n_words': 366}