Last active
March 4, 2017 13:05
-
-
Save stephencarr/019a4ae51b4b643959120fdc92efe985 to your computer and use it in GitHub Desktop.
Ember authorizer for simple-auth + devise-auth-token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// `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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// `/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); | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment