Skip to content

Instantly share code, notes, and snippets.

@thiagosbrito
Last active October 6, 2015 14:38
Show Gist options
  • Save thiagosbrito/37bdbb2b6c89c129ea8a to your computer and use it in GitHub Desktop.
Save thiagosbrito/37bdbb2b6c89c129ea8a to your computer and use it in GitHub Desktop.
LoginController.js
angular.module("emailOverviewApp").factory('requestInterceptor', [
'$rootScope', '$q', '$log', function($rootScope, $q, $log) {
var checkSession, noCache, setRequestHeader;
noCache = function(url) {
return url + "?_=" + (Math.random());
};
checkSession = function(request) {
if (request.status === 401) {
// Feedback.fail("Não autenticado");
$rootScope.$emit("loginRequired");
$rootScope.isLogado = false;
}
if (request.status === 0) {
$rootScope.$emit("loginRequired");
return $rootScope.isLogado = false;
}
};
setRequestHeader = function(config) {
if (config.method === 'DELETE') {
config.headers['X-HTTP-Method-Override'] = config.method;
}
/*if config.method is 'DELETE'
config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
if !config.data
config.data =
_method: config.method
config.data = $.param config.data
config.method = 'POST'
*/
};
return {
request: function(config) {
setRequestHeader(config);
return config || $q.when(config);
},
requestError: function(rejection) {
return $q.reject(rejection);
},
response: function(response) {
return response || $q.when(response);
},
responseError: function(rejection) {
var msg;
checkSession(rejection);
// $log.error('[EMAIL-OVERVIEW]Status Code : ', rejection.status);
// $log.error('[EMAIL-OVERVIEW]Status Text : ', rejection.statusText);
// msg = "Erro: " + rejection.status + ", " + rejection.statusText;
return $q.reject(rejection);
}
};
}
]);
angular.module('emailOverviewApp')
.controller('LoginController', ['$scope','$state','LoginService',function ($scope,$state,LoginService ) {
$scope.loginForm = {};
$scope.login = function () {
// $state.go('tab.projetos.listar')
LoginService.login($scope.loginForm).then(
function (data) {
// $state.go('tab.projetos.listar')
LoginService.session().then(function(result) {
LoginService.setUser(result.data);
$state.go('tab.projetos.listar')
});
}),
function (error) {
console.log(error)
}
}
}]);
angular.module('emailOverviewApp').service('LoginService', [
'$rootScope', '$http', function($rootScope, $http) {
var user;
user = {};
return {
login: function(params) {
return $http({
url: "/api/login/authenticate?username="+params.username+"&password="+params.password,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
});
},
session: function() {
return $http.get("/api/usuario");
},
setUser: function(session) {
return user = session;
},
getUser: function() {
return user || null;
},
checkSession: function() {
var that;
that = this;
return this.session().then(function(result) {
return that.setUser(result.data);
}, function(error) {
if (error.status === 500) {
return $rootScope.$emit('sessionFailed');
}
});
},
atualizaUsuario: function(dadosPessoais, id) {
return $http({
url: ("/api/usuario/") + id,
method: "PUT",
data: dadosPessoais,
headers: {
"Content-Type": "application/json"
}
});
}
};
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment