Skip to content

Instantly share code, notes, and snippets.

@jlstr
Created August 22, 2014 17:40
Show Gist options
  • Save jlstr/356cc5b17e30aec6417b to your computer and use it in GitHub Desktop.
Save jlstr/356cc5b17e30aec6417b to your computer and use it in GitHub Desktop.
'use strict';
(function() {
var app = angular.module('services', []);
app.factory('AuthenticationService', ['$http', '$q', '$localStorage',
function($http, $q, $localStorage) {
return {
login: function(credentials) {
var promise = $http.post('http://localhost:3000/api/users/sign_in',
credentials).
then(function(response) {
if(response.data.success) {
//respond with currentUser(response.data)
return response.data;
}
}, function(response) {
console.log('FAILURE!');
return $q.reject(response.data);
});
return promise;
},
clearStorage: function() {
var defer = $q.defer();
if($localStorage.$reset()) {
defer.resolve('storage cleared.');
}
return defer.promise;
},
logout: function() {
var promise = $http.delete('http://localhost:3000/api/users/sign_out').
then(function(response) {
if(response.data.success) {
return response.data;
}
}, function(response) {
console.log('FAILURE IN SIGN OUT');
return $q.reject(response.data);
});
return promise;
},
isSignedIn: function() {
var currentUser = $localStorage.user;
var signedIn = (currentUser === undefined || currentUser === null) ?
false : true;
return signedIn;
},
getCurrentUser: function() {
if(this.isSignedIn()) {
var currentUser = $localStorage.user;
return currentUser;
} else {
return null;
}
}
};
}]);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment