Created
August 29, 2014 23:18
-
-
Save justincjahn/2223d4e916428507ea42 to your computer and use it in GitHub Desktop.
jQuery OAuth2 Bearer Login
This file contains 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
$(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