Last active
January 17, 2017 19:57
-
-
Save nickschot/9e1443e395c53bec6f94beae711effed to your computer and use it in GitHub Desktop.
Modified ember-simple-auth-token JWT authenticator suitable for Lux
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
import Ember from 'ember'; | |
import JWT from 'ember-simple-auth-token/authenticators/jwt'; | |
const assign = Ember.assign || Ember.merge; | |
export default JWT.extend({ | |
/** | |
Accepts a `url` and `data` to be used in an ajax server request. | |
@method makeRequest | |
@private | |
*/ | |
makeRequest(url, data, headers) { | |
return Ember.$.ajax({ | |
url: url, | |
method: 'POST', | |
data: JSON.stringify({ | |
"data": { | |
"type": "auth", | |
"attributes": data | |
} | |
}), | |
dataType: 'json', | |
contentType: 'application/vnd.api+json', | |
headers: this.headers, | |
beforeSend: (xhr, settings) => { | |
if(this.headers['Accept'] === null || this.headers['Accept'] === undefined) { | |
xhr.setRequestHeader('Accept', settings.accepts.json); | |
} | |
if (headers) { | |
Object.keys(headers).forEach((key) => { | |
xhr.setRequestHeader(key, headers[key]); | |
}); | |
} | |
} | |
}); | |
}, | |
/** | |
Handles authentication response from server, and returns session data | |
@method handleAuthResponse | |
@private | |
*/ | |
handleAuthResponse(response) { | |
const token = Ember.get(response, this.tokenPropertyName); | |
if (Ember.isEmpty(token)) { | |
throw new Error('Token is empty. Please check your backend response.'); | |
} | |
const tokenData = this.getTokenData(token); | |
const expiresAt = Ember.get(tokenData, this.tokenExpireName); | |
const user = Ember.get(tokenData, 'user'); | |
const tokenExpireData = {}; | |
tokenExpireData[this.tokenExpireName] = expiresAt; | |
tokenExpireData['user'] = user; | |
this.scheduleAccessTokenRefresh(expiresAt, token); | |
return assign(this.getResponseData(response), tokenExpireData); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment