Created
July 9, 2014 12:48
-
-
Save jsomara/e62bfb13b0dbc3117636 to your computer and use it in GitHub Desktop.
auth factory
This file contains hidden or 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
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