Skip to content

Instantly share code, notes, and snippets.

@justincjahn
Created August 29, 2014 23:18
Show Gist options
  • Save justincjahn/2223d4e916428507ea42 to your computer and use it in GitHub Desktop.
Save justincjahn/2223d4e916428507ea42 to your computer and use it in GitHub Desktop.
jQuery OAuth2 Bearer Login
$(function () {
$('#login').submit(function (e) {
e.preventDefault();
var formData = {
'grant_type': 'password',
'username': $('#username').val(),
'password': $('#password').val(),
'client_id': 'unknown',
'client_secret': 'unknown'
};
$.ajax({
type: 'POST',
url: '/oauth/token',
data: formData,
dataType: 'json'
}).done(function(data) {
token = data.access_token;
$('#test').html('Login sucessful!: ' + token);
}).fail(function(res) {
if (res.status >= 400 && res.status < 500) {
$('#test').html('Invalid username or password.');
} else {
$('#test').html('An unknown error occurred: ' . res.responseJSON.error_description);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment