Skip to content

Instantly share code, notes, and snippets.

@pbedat
Created July 6, 2014 11:48
Show Gist options
  • Select an option

  • Save pbedat/fc5e4351081e8520a173 to your computer and use it in GitHub Desktop.

Select an option

Save pbedat/fc5e4351081e8520a173 to your computer and use it in GitHub Desktop.
angular
.module("xSources.Security")
.service("AuthenticationApi", function ($rootScope, ipCookie, SessionModel, Restangular) {
var my = this;
my.initialize = function () {
SessionModel.get().then(function (session) {
if (session == null || !session.IsValid) {
$rootScope.$broadcast("authentication:required");
} else {
session.logout = my.logout;
$rootScope.$broadcast("authentication:granted", session);
}
});
};
my.login = function (user, pass) {
Restangular.all("UserSession").post({
User: {
Name: user,
Password: pass
}
}).then(function (session) {
if (session.IsValid) {
SessionModel.create(session);
session.logout = my.logout;
$rootScope.$broadcast("authentication:granted", session);
} else {
$rootScope.$broadcast("authentication:denied");
}
}, function (res) {
$rootScope.$broadcast("authentication:failed", res);
});
};
my.logout = function () {
SessionModel.destroy().then(function () {
window.location.reload();
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment