Skip to content

Instantly share code, notes, and snippets.

@Deiru2k
Created September 24, 2015 09:05
Show Gist options
  • Save Deiru2k/1d3bd53c148e27f1fa2d to your computer and use it in GitHub Desktop.
Save Deiru2k/1d3bd53c148e27f1fa2d to your computer and use it in GitHub Desktop.
SUPER AUTH
auth.singInWebapi = (user) => {
return new $q((resolve, reject) => {
const address = `${$API.url}Token`;
const request = new XMLHttpRequest;
const handleResponse = () => {
const { status } = request;
if (request.readyState === request.DONE) {
if (status > 0) {
const body = JSON.parse(request.response);
const { access_token, error, error_description } = body;
if (!(error || error_description)) {
$rootScope.$broadcast('tokenReceived', access_token);
resolve(body);
} else {
reject([error_description || error]);
}
} else {
reject(Error('Request Failed'));
}
}
};
const creds = {
username: user.Email,
password: user.Password,
grant_type: 'password'
};
const data = `username=${creds.username}&password=${creds.password}&grant_type=${creds.grant_type}`;
request.onreadystatechange = handleResponse;
request.open('POST', address)
$API.httpHeaders.forEach((value, key) => {
request.setRequestHeader(key, value);
});
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
request.send(data);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment