Skip to content

Instantly share code, notes, and snippets.

@jsomara
Created July 9, 2014 12:48
Show Gist options
  • Save jsomara/e62bfb13b0dbc3117636 to your computer and use it in GitHub Desktop.
Save jsomara/e62bfb13b0dbc3117636 to your computer and use it in GitHub Desktop.
auth factory
angular.module('Arc')
.factory('basicAuth', ['$cookieStore', '$http', 'Base64', function ($cookieStore, $http, Base64) {
return {
setCredentials: function (token) {
var encoded = Base64.encode(token + ':X');
$http.defaults.headers.common.Authorization = 'Basic ' + encoded;
$cookieStore.put('authdata', encoded);
},
clearCredentials: function () {
document.execCommand("ClearAuthenticationCache");
$cookieStore.remove('authdata');
$http.defaults.headers.common.Authorization = 'Basic ';
}
};
}])
.factory('authService', ['$state','httpBuffer', 'basicAuth', function($state, httpBuffer, basicAuth) {
return {
/**
* call this function to indicate that authentication was successfull and trigger a
* retry of all deferred requests.
* @param data an optional argument to pass on to $broadcast which may be useful for
* example if you need to pass through details of the user that was logged in
*/
loginConfirmed: function(data) {
basicAuth.setCredentials(data.auth_token);
$state.transitionTo('characters');
}
};
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment