Skip to content

Instantly share code, notes, and snippets.

@MichalBryxi
Last active May 28, 2020 19:05
Show Gist options
  • Save MichalBryxi/c4ecb2896af247cc36c0faa37556b55a to your computer and use it in GitHub Desktop.
Save MichalBryxi/c4ecb2896af247cc36c0faa37556b55a to your computer and use it in GitHub Desktop.
devise-token-auth authenticator for ember-simple-auth
// file: app/authenticators/devise-token-auth.js
// fix for: https://github.com/simplabs/ember-simple-auth/issues/2206
import Devise from "ember-simple-auth/authenticators/devise";
import { Promise } from "rsvp";
import { isEmpty } from "@ember/utils";
import { run } from "@ember/runloop";
import { merge, assign as emberAssign } from "@ember/polyfills";
import fetch from "fetch";
export default Devise.extend({
authenticate(identification, password) {
return new Promise((resolve, reject) => {
const {
resourceName,
identificationAttributeName,
tokenAttributeName,
} = this.getProperties(
"resourceName",
"identificationAttributeName",
"tokenAttributeName"
);
const data = {};
data["password"] = password;
data[identificationAttributeName] = identification;
this.makeRequest(data)
.then((response) => {
if (response.ok) {
if (this._validate(response)) {
let json = {};
json[identificationAttributeName] = response.headers.get("uid");
json[tokenAttributeName] = response.headers.get("access-token");
run(null, resolve, json);
} else {
run(
null,
reject,
`Check that server response includes ${tokenAttributeName} and ${identificationAttributeName}`
);
}
} else {
run(null, reject, response);
}
})
.catch((error) => run(null, reject, error));
});
},
_validate(response) {
return response.headers.get("uid") && response.headers.get("access-token");
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment