Skip to content

Instantly share code, notes, and snippets.

@stephencarr
Last active March 4, 2017 13:05
Show Gist options
  • Save stephencarr/019a4ae51b4b643959120fdc92efe985 to your computer and use it in GitHub Desktop.
Save stephencarr/019a4ae51b4b643959120fdc92efe985 to your computer and use it in GitHub Desktop.
Ember authorizer for simple-auth + devise-auth-token
// `adapters/application.js`
import Ember from 'ember';
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
const { service } = Ember.inject;
import config from '../config/environment';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
authorizer: 'authorizer:devise',
host: `${config.apiHost}`,
namespace: `${config.apiNamespace}`,
session: service('session'),
handleResponse(status, headers) {
if(headers['client']) {
let newSession = this.get('session.data');
newSession['authenticated']['accessToken'][0] = headers['access-token'];
newSession['authenticated']['expiry'][0] = headers['expiry'];
newSession['authenticated']['tokenType'][0] = headers['token-type'];
newSession['authenticated']['uid'][0] = headers['uid'];
newSession['authenticated']['client'][0] = headers['client'];
this.get('session.store').persist(newSession);
} else if (status == 401) {
this.get('session').invalidate();
}
return this._super(...arguments);
}
});
// `/authenticators/authenticator_devise.js`
import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
import Ember from 'ember';
import config from '../config/environment';
const { RSVP, isEmpty, run } = Ember;
export default DeviseAuthenticator.extend({
session: Ember.inject.service('session'),
serverTokenEndpoint: `${config.apiHost}/${config.apiNamespace}/auth/sign_in`,
restore(data){
return new RSVP.Promise((resolve, reject) => {
if (!isEmpty(data.accessToken) && !isEmpty(data.expiry) &&
!isEmpty(data.tokenType) && !isEmpty(data.uid) && !isEmpty(data.client)) {
resolve(data);
} else {
reject();
}
});
},
authenticate(identification, password) {
return new RSVP.Promise((resolve, reject) => {
const { identificationAttributeName } = this.getProperties('identificationAttributeName');
const data = { password };
data[identificationAttributeName] = identification;
this.makeRequest(data).then(function(response) {
if(response.status != 401) {
var result = {
accessToken: response.headers.map['access-token'],
expiry: response.headers.map['expiry'],
tokenType: response.headers.map['token-type'],
uid: response.headers.map['uid'],
client: response.headers.map['client']
};
run(null, resolve, result);
} else {
this.get('session').invalidate();
}
}, function(xhr) {
run(null, reject, xhr.responseJSON || xhr.responseText);
});
});
}
});
// `/authorizers/authorizer_devise.js`
import Ember from 'ember';
import Base from 'ember-simple-auth/authorizers/base';
const { service } = Ember.inject;
export default Base.extend({
session: service('session'),
authorize(data, block) {
const accessToken = data.accessToken[0];
const expiry = data.expiry[0];
const tokenType = data.tokenType[0];
const uid = data.uid[0];
const client = data.client[0];
if (this.get('session.isAuthenticated') && !Ember.isEmpty(accessToken)) {
block('Authorization', `Bearer ${accessToken}`);
block('access-token', accessToken);
block('expiry', expiry);
block('token-type', tokenType);
block('uid', uid);
block('client', client);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment