| title | jQuery Tutorial | ||||||
|---|---|---|---|---|---|---|---|
| name | jQuery | ||||||
| alias |
|
||||||
| language |
|
||||||
| framework |
|
||||||
| image | //upload.wikimedia.org/wikipedia/en/9/9e/JQuery_logo.svg | ||||||
| tags |
|
||||||
| snippets |
|
You can get started by either downloading the seed project or if you would like to add Auth0 to an existing application you can follow the tutorial steps.
::: panel-info System Requirements This tutorial and seed project have been tested with the following:
- NodeJS 4.3
- Jquery 2.1.1 :::
<%= include('../_includes/_package', { pkgRepo: 'auth0-jquery', pkgBranch: 'gh-pages', pkgPath: (configuration.thirdParty) ? 'examples/widget-with-thirdparty-api' : 'examples/widget-with-api-redirect', pkgFilePath: null, pkgType: 'js' }) %>
If you have an existing application, follow the steps below.
${include('./_callback')}
Add the code below to the index.html file to include Auth0's jQuery module and its dependencies and set the viewport:
${snippet(meta.snippets.dependencies)}
Configure Auth0Lock with your client-ID and domain:
${snippet(meta.snippets.setup)}
To implement the login, call the .show() method of Auth0's lock instance when a user clicks the login button.
Note: This implementation uses Lock's redirect mode.
${snippet(meta.snippets.use)}
To discover all the available arguments for lock.show, see .show([options, callback]).
After authentication, Auth0 will redirect the user back to your application with an identifying token as a hash parameter of window.location. Use lock.parseHash to parse the hash and create the token. This token is used to retrieve the user's profile from Auth0 and to call your backend APIs.
In this example, the id_token is stored in localStorage to keep the user authenticated after each page refresh.
var hash = lock.parseHash(window.location.hash);
if (hash) {
if (hash.error) {
console.log("There was an error logging in", hash.error);
alert('There was an error: ' + hash.error + '\n' + hash.error_description);
} else {
//save the token in the session:
localStorage.setItem('id_token', hash.id_token);
}
}${browser}
<% if (configuration.thirdParty) { %>
To enable calls to a third-party API <%= configuration.api %>, exchange the JWT token from Auth0 for a token that can be used to query <%= configuration.api %> securely.
Modify the login code in Step 3 by adding a call to get the new token:
lock.getClient().getDelegationToken({
id_token: token,
// By default the first active third party add-on will be used
// However, We can specify which third party API to use here by specifying the name of the add-on
// api: <%= configuration.api %>
},
function(err, thirdPartyApiToken) {
localStorage.setItem('thirdPartyApiToken', thirdPartyApiToken.id_token);
});The code above will function once the <%= configuration.api %> add-on is activated in the following steps.
<% } else { %>
To configure secure calls to the API you are creating <%= configuration.api ? ' on ' + configuration.api : '' %>, implement $.ajaxSetup to send on each request, in the Authorization header with every ajax call, the JWT token received on the login and saved to localStorage as shown in Step 3.
$.ajaxSetup({
'beforeSend': function(xhr) {
if (localStorage.getItem('id_token')) {
xhr.setRequestHeader('Authorization',
'Bearer ' + localStorage.getItem('id_token'));
}
}
});Note: The settings specified in ajaxSetup will affect all calls to
<% } %>
Use the id_token to retrieve the user profile and display the user's nickname:
//retrieve the profile:
var id_token = localStorage.getItem('id_token');
if (id_token) {
lock.getProfile(id_token, function (err, profile) {
if (err) {
return alert('There was an error getting the profile: ' + err.message);
}
// Display user information
$('.nickname').text(profile.nickname);
});
}<p>Welcome <span class="nickname"></span></p>To discover all the available properties of a user's profile, see Auth0 Normalized User Profile. Note that the properties available depend on the social provider used.
In this implementation, a log out involves simply deleting the saved token from localStorage and redirecting the user to the home page:
localStorage.removeItem('id_token');
userProfile = null;
window.location.href = "/";You have completed the implementation of Login and Signup with Auth0 and jQuery.