This is an extension to the Ember Simple Auth library that provides an authenticator and an authorizer that are compatible with APIs with token-based authentication.
As your user's credentials as well as the token are exchanged between the Ember.js app and the server you have to make sure that this connection uses HTTPS!
Based on ember-simple-auth-devise.
To install Ember Simple Auth Token in an Ember.js application that uses Ember CLI:
Make sure you have ember-cli-simple-auth installed:
npm install --save-dev ember-cli-simple-auth
ember generate ember-cli-simple-auth
To install simply run:
npm install --save-dev ember-cli-simple-auth-token
ember generate simple-auth-token
In order to use the Token authenticator the application needs to have a login route:
// app/router.js
Router.map(function() {
this.route('login');
});
This route displays the login form with fields for identification
,
password
:
{{! app/templates/login.hbs }}
<form {{action 'authenticate' on='submit'}}>
<label for="identification">Login</label>
{{input id='identification' placeholder='Enter Login' value=identification}}
<label for="password">Password</label>
{{input id='password' placeholder='Enter Password' type='password' value=password}}
<button type="submit">Login</button>
</form>
The authenticate
action that is triggered by submitting the form is provided
by the LoginControllerMixin
that the respective controller in the application
can include (the controller can also implement its own action and use the
session API directly; see the
API docs for Session
).
It then also needs to specify the Token authenticator to be used:
// app/controllers/login.js
import Ember from 'ember';
import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';
export default Ember.Controller.extend(LoginControllerMixin, {
authenticator: 'simple-auth-authenticator:token'
});
The authorizer authorizes requests by adding token
property from the session in the Authorization
header:
Authorization: Bearer <token>
To use the authorizer, configure it in the global environment object:
// config/environment.js
ENV['simple-auth'] = {
authorizer: 'simple-auth-authorizer:token'
};
// config/environment.js
ENV['simple-auth-token'] = {
serverTokenEndpoint: '/api-token-auth/',
identificationField: 'username',
passwordField: 'password',
tokenPropertyName: 'token',
authorizationPrefix: 'Bearer ',
authorizationHeaderName: 'Authorization',
headers: {}
};