Skip to content

Instantly share code, notes, and snippets.

@rmaceissoft
Created December 24, 2013 05:40
Show Gist options
  • Save rmaceissoft/8109206 to your computer and use it in GitHub Desktop.
Save rmaceissoft/8109206 to your computer and use it in GitHub Desktop.
angularjs service to handle login/logout/status
define(function(require, exports, module) {
var angular = require('angular');
require('angular-cookies');
var usersModule = module.exports = angular.module('users', ['ngCookies'])
.factory('UserService', ['$http', '$cookieStore', function($http, $cookieStore) {
var currentUser = $cookieStore.get('user') || null;
$cookieStore.remove('user');
function changeUser(user) {
currentUser = user;
}
return {
/*
determines if current user is logged in, checking cookies
*/
isLoggedIn: function(user) {
if (user === undefined) {
user = currentUser;
}
return user ? true : false;
},
login: function(user, success, error) {
$http.post('/login', user).success(function(user){
changeUser(user);
success(user);
}).error(error);
},
logout: function(success, error) {
$http.post('/logout').success(function(){
changeUser(null);
success();
}).error(error);
}
};
}]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment