lodash | title | name | image | tags | |
---|---|---|---|---|---|
true |
EmberJS Tutorial |
EmberJS |
//auth0.com/lib/platforms-collection/img/emberjs.png |
|
Otherwise, if you already have an existing application, please follow the steps below.
@@includes.callback@@
auth0-ember-simple-auth
is an add-on for simple-auth, which can be installed via ember-cli.
cd
to your project directory, then run the following command to install both add-ons and their dependencies:
ember install auth0-ember-simple-auth
ember generate simple-lock
Note: If you're not already using ember-cli, then you may wish to read the handy ember-cli guide page.
Once you've installed everything, its just a matter of adding some configuration.
// config/environment.js
ENV['simple-auth'] = {
authorizer: 'simple-auth-authenticator:lock',
authenticationRoute: 'sign_in',
routeAfterAuthentication: 'home',
routeIfAlreadyAuthenticated: 'home'
}
ENV['simple-lock'] = {
clientID: "<%= account.clientId %>",
domain: "<%= account.namespace %>"
}
Note: If you're using a content security policy, you'll also need to add
<%= account.namespace %>
to theconnect-src
, and the following entries:https://cdn.auth0.com
to thefont-src
andscript-src
CSP entries.
// config/environment.js
ENV['contentSecurityPolicy'] = {
'font-src': "'self' data: https://cdn.auth0.com",
'style-src': "'self' 'unsafe-inline'",
'script-src': "'self' 'unsafe-eval' 'unsafe-inline' https://cdn.auth0.com",
'connect-src': "'self' http://localhost:* <%= account.namespace %>"
};
// app/routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin, {
actions: {
sessionRequiresAuthentication: function(){
// Check out the docs for all the options:
// https://auth0.com/docs/libraries/lock/customization
// This will launch lock.js in popup mode
var lockOptions = {authParams:{scope: 'openid'}};
this.get('session').authenticate('simple-auth-authenticator:lock', lockOptions);
}
}
});
Now that we've mixed in ApplicationRouteMixin
, all we need to do is add a route for signing in, and a route for where the user will be directed once signed in.
// app/routes/sign_in.js
import Ember from 'ember';
import UnauthenticatedRouteMixin from 'simple-auth/mixins/unauthenticated-route-mixin';
export default Ember.Route.extend(UnauthenticatedRouteMixin);
// app/routes/home.js
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin);
Now that your router is ready, you'll probably want to add some links so that your users can sign in and out. These routes are handled by the configuration that we added for ember simple auth.
When you authenticate, your application will receive session data from the popup window — Then, session data will be stored in localStorage with a key of ember_simple_auth:session
. The session object is a JSON object that contains your user profile data, JWT token and access token.
You can access this session information in ember templates by using {{session.secure}}
. So for example, if you wanted to say Hi to the user and show the associated avatar:
When you want to make an API request, you will need to add the users JWT token to an Authorization
HTTP header:
fetch('/api/foo', {
method: 'GET',
cache: false,
headers: {
'Authorization': 'Bearer <%= "${session.secure.jwt}" %>'
}
}).then(function (response) {
// use response
});
🙌 You've implemented Sign in & up with Auth0 and Ember.
To get Additional info on how to use this SDK, check the readme