Created
August 22, 2014 17:40
-
-
Save jlstr/356cc5b17e30aec6417b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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