Created
December 24, 2013 05:40
-
-
Save rmaceissoft/8109206 to your computer and use it in GitHub Desktop.
angularjs service to handle login/logout/status
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
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