Skip to content

Commit ee1c20d

Browse files
committed
Move auth stuff into beforeModel so we can use Ember events/models
1 parent a3c91b1 commit ee1c20d

File tree

2 files changed

+63
-80
lines changed

2 files changed

+63
-80
lines changed

app/app_setup.js

Lines changed: 24 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,31 @@
33

44
window.balancedSetupFunctions = [];
55

6-
7-
function hackTheLogin () {
8-
if (window.TESTING) {
9-
return;
10-
}
11-
12-
// TODO: refactor this nastiness.
13-
// what happens is that ember-auth uses async: false which does not work in
14-
// firefox. so instead, what we do is hack in auth **before** the app begins
15-
// working. this means that if the user is already logged in then ember-auth
16-
// is going to think that they are all good to go and the async: false
17-
// won't break our flow.
18-
window.Balanced.deferReadiness();
19-
20-
function fin () {
21-
window.Balanced.advanceReadiness();
22-
}
23-
24-
$.ajax({
25-
type: 'POST',
26-
url: Ember.ENV.BALANCED.AUTH,
27-
xhrFields: {
28-
withCredentials: true
29-
}
30-
}).success(function (r) {
31-
var csrfToken = r.csrf;
32-
Balanced.NET.ajaxHeaders['X-CSRFToken'] = csrfToken;
33-
var authCookie = Balanced.Auth.retrieveLogin();
34-
if (authCookie) {
35-
$.ajax('https://auth.balancedpayments.com/logins', {
36-
type: 'POST',
37-
xhrFields: {
38-
withCredentials: true
39-
},
40-
data: { uri: authCookie }
41-
}).success(function (login) {
42-
Balanced.Analytics.trackEvent('login-success', {remembered: true});
43-
44-
// set the auth stuff manually
45-
Balanced.Auth.setAuthProperties(
46-
true,
47-
Balanced.User.find(login.user_uri),
48-
login.user_id,
49-
login.user_id,
50-
false
51-
);
52-
fin();
53-
}).error(fin);
54-
} else {
55-
fin();
56-
}
57-
}).error(fin);
58-
}
59-
606
/*
61-
Creates a new instance of an Ember application and
62-
specifies what HTML element inside index.html Ember
63-
should manage for you.
64-
*/
7+
Creates a new instance of an Ember application and
8+
specifies what HTML element inside index.html Ember
9+
should manage for you.
10+
*/
6511
window.setupBalanced = function (divSelector) {
6612

67-
// default to #balanced-app if not specified
68-
divSelector = divSelector || '#balanced-app';
69-
window.Balanced = Ember.Application.create({
70-
rootElement: divSelector,
71-
LOG_TRANSITIONS: true,
72-
73-
customEvents: {
74-
// key is the jquery event, value is the name used in views
75-
changeDate: 'changeDate'
76-
}
77-
});
78-
79-
hackTheLogin();
80-
81-
window.Balanced.onLoad = function () {
82-
// initialize anything that needs to be done on application load
83-
Balanced.Analytics.init(Ember.ENV.BALANCED);
84-
};
85-
86-
_.each(window.balancedSetupFunctions, function (setupFunction) {
87-
setupFunction();
88-
});
13+
// default to #balanced-app if not specified
14+
divSelector = divSelector || '#balanced-app';
15+
window.Balanced = Ember.Application.create({
16+
rootElement: divSelector,
17+
LOG_TRANSITIONS: true,
18+
19+
customEvents: {
20+
// key is the jquery event, value is the name used in views
21+
changeDate: 'changeDate'
22+
}
23+
});
24+
25+
window.Balanced.onLoad = function () {
26+
// initialize anything that needs to be done on application load
27+
Balanced.Analytics.init(Ember.ENV.BALANCED);
28+
};
29+
30+
_.each(window.balancedSetupFunctions, function (setupFunction) {
31+
setupFunction();
32+
});
8933
};

app/routes/application.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
Balanced.ApplicationRoute = Balanced.Route.extend({
2+
beforeModel: function() {
3+
if (window.TESTING || Balanced.Auth.get('signedIn')) {
4+
return;
5+
}
6+
7+
var self = this;
8+
9+
return $.ajax({
10+
type: 'POST',
11+
url: Ember.ENV.BALANCED.AUTH,
12+
xhrFields: {
13+
withCredentials: true
14+
}
15+
}).then(function(response, status, jqxhr) {
16+
var csrfToken = response.csrf;
17+
Balanced.NET.ajaxHeaders['X-CSRFToken'] = csrfToken;
18+
19+
var authCookie = Balanced.Auth.retrieveLogin();
20+
if (authCookie) {
21+
return $.ajax('https://auth.balancedpayments.com/logins', {
22+
type: 'POST',
23+
xhrFields: {
24+
withCredentials: true
25+
},
26+
data: { uri: authCookie }
27+
}).success(function (response, status, jqxhr) {
28+
// set the auth stuff manually
29+
Balanced.Auth.setAuthProperties(
30+
true,
31+
Balanced.User.find(response.user_uri),
32+
response.user_id,
33+
response.user_id,
34+
false
35+
);
36+
});
37+
}
38+
});
39+
},
40+
241
events: {
342
error: function(error, transition) {
443
Ember.Logger.error("Error while loading route (%@: %@): ".fmt(error.errorStatusCode, error.uri), error.stack || error);

0 commit comments

Comments
 (0)