Skip to content

Instantly share code, notes, and snippets.

@Robinyo
Last active August 29, 2015 14:25
Show Gist options
  • Save Robinyo/7167e1cbdd2d14a5280a to your computer and use it in GitHub Desktop.
Save Robinyo/7167e1cbdd2d14a5280a to your computer and use it in GitHub Desktop.
'use strict';
angular.module('vardyger')
.factory('AuthenticationService',
function(
$log, // inject the $log service
$rootScope, // inject the $rootScope service
$http, // inject the $http service
authService, // inject the authService service
localStorageService // inject the localStorageService service
) {
$log.info('AuthenticationService');
var loggedIn = false;
var service = {
login: function(credentials) {
$http.post('https://login', { user: credentials }, { ignoreAuthModule: true })
.success(function (data, status, headers, config) {
loggedIn = true;
$http.defaults.headers.common.Authorization = data.authorizationToken;
localStorageService.set('authorizationToken', data.authorizationToken);
authService.loginConfirmed(data, function(config) {
config.headers.Authorization = data.authorizationToken;
return config;
});
})
.error(function (data, status, headers, config) {
$rootScope.$broadcast('event:auth-login-failed', status);
});
},
isLoggedIn: function() {
return loggedIn;
},
loginCancelled: function() {
authService.loginCancelled();
},
logout: function() {
loggedIn = false;
$http.post('https://logout', {}, { ignoreAuthModule: true })
.finally(function(data) {
localStorageService.remove('authorizationToken');
delete $http.defaults.headers.common.Authorization;
$rootScope.$broadcast('event:auth-logout-complete');
});
}
};
return service;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment