Skip to content

Instantly share code, notes, and snippets.

@rstudner
Last active August 29, 2015 14:01
Show Gist options
  • Save rstudner/3a38b5be3f73985b5385 to your computer and use it in GitHub Desktop.
Save rstudner/3a38b5be3f73985b5385 to your computer and use it in GitHub Desktop.
import {request, raw} from 'ic-ajax';
var ApplicationRoute = Ember.Route.extend({
beforeModel: function() {
},
model: function () {
return new Ember.RSVP.Promise(function (resolve, reject) {
var promise = new Ember.RSVP.Promise(function (p1Resolve, reject) {
request({
dataType: 'json',
url: window.ENV.prependHost + "/sa/api/v2/auth"
})
.then(resolve) // if the first is a success, just resolve!
.catch(function () { // not a success, trying again
Ember.Logger.error('First auth attempt failed, lets just do admin');
request({
dataType: 'json',
url: window.ENV.prependHost + "/sa/api/v2/auth",
headers: {
withCredentials: true,
Authorization: 'Basic YWRtaW46YWRtaW4='
}
})
.then(p1Resolve) // the second is a success, resolve!
.catch(reject); // the second failed :( reject
});
});
var promise2 = new Ember.RSVP.Promise(function (resolve, reject) {
promise.then(function (data) {
Ember.Logger.error("now calling 'then' on the first promise");
Ember.Logger.error(data);
var username = data.auth.id;
var token = data.auth.secret;
var headerValue = username + ':' + token;
request({
dataType: 'json',
url: window.ENV.prependHost + "/sa/api/v2/users/2",
headers: {
withCredentials: true,
Authorization: 'Basic ' + btoa(headerValue)
}
}).then(resolve).catch(reject);
});
});
promise2.then(function (data) {
Ember.Logger.error("made it to here as well holy smokes");
Ember.Logger.error(data);
resolve(data);
}).catch(function (failure) {
// either we couldn't auth
// or the user data wasn't retrieved.
// in a simple case, we don't know what ...
// but you could reject with an arg to indicate which
});
});
},
afterModel: function (authObject, transition) {
console.log("AfterModel:");
Ember.Logger.debug(authObject);
//Ember.Logger.debug("token: " + authObject.auth.secret);
}
});
export default ApplicationRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment