Skip to content

Instantly share code, notes, and snippets.

@fedorovsky
Created March 1, 2017 08:15
Show Gist options
  • Save fedorovsky/0bbaea93dcf66d4cad031867a0224641 to your computer and use it in GitHub Desktop.
Save fedorovsky/0bbaea93dcf66d4cad031867a0224641 to your computer and use it in GitHub Desktop.
angular.module('authApp', ['ngStorage', 'ngMessages'])
.factory('httpInterceptor', function ($rootScope, $q, $log) {
return {
'request' : function (config) {
config.timeout = 1000;
// Данные которые нужно подмешать в каждый запрос
var data = {
api_token: '__token__',
lang : 'en'
};
config.data = angular.toJson(angular.extend(angular.fromJson(config.data), data));
return config;
},
'response' : function (config) {
return config;
},
'responseError': function (rejection) {
return $q.reject(rejection);
}
};
})
.config(function ($httpProvider, $qProvider) {
$httpProvider.interceptors.push('httpInterceptor');
$qProvider.errorOnUnhandledRejections(false);
})
.controller('AuthCtrl', function ($scope, $http, $log, $localStorage, $location) {
$scope.vm = {
email : '',
password : '',
error : false,
errorMessage: '',
sign : signIn
};
/**
* Sign in handler
* @param email
* @param password
*/
function signIn(email, password) {
$log.debug('LocalStorage -> ', $localStorage.userInfo);
var data = {
email : email,
password: password
};
if ($scope.loginForm.$valid) {
$http.post('http://46.101.254.89/api/v1/user/login', data).then(function (response) {
// Удачный запрос
if (response.status === 200 && response.data.data.api_token) {
$localStorage.userInfo = angular.fromJson(response.data.data);
window.location.href = '/admin/main';
}
})
.catch(function (response) {
$log.error(response.data.message);
$scope.vm.error = true;
$scope.vm.errorMessage = response.data.message;
});
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment