Making sure that your users are logged in with your Ember.js app can be fiddly to get right and often involves boilerplate code that gets shared between projects. In particular supporting multiple authentication methods can be a nightmare.

The library ember-simple-auth standardises the logic for authenticating the user and adding session information to subsequent requests. This is done through what they call Authenticators and Authorizers respectively.

It has a bunch of generic authenticators and authorizers defined for common setups including OAuth 2, Torii and Devise. You can roll your own authenticator by defining how to validate, restore and invalidate a session with the server.

This provides a mixin that you can add to your ApplicationRoute which will give you access to a session object in your application. E.g.

invalidateSession: function() {  
{{#if session.isAuthenticated}}

Installation is simple enough, there is an ember-cli addon which sets up all the imports etc. E.g. if you're using Devise with Rails and ember-cli:

ember install ember-cli-simple-auth  
ember install ember-cli-simple-auth-devise  

Add the application route mixin to get access to the session object.

// app/routes/application.js
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin);  

Endpoints and authorizers are configured in config/environment.js:

ENV['simple-auth'] = {  
  authorizer: 'simple-auth-authorizer:devise'

ENV['simple-auth-devise'] = {  
  serverTokenEndpoint: 'admin/users/sign_in'

Finally when process your login form, call the authenticate method on the session.

let data = this.getProperties('email', 'password');  
this.get('session').authenticate('simple-auth-authenticator:devise', data);  
Tags: Addons