This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
login.js
299 lines (267 loc) · 10.6 KB
/
login.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
define('core/login',
['core/cache', 'core/capabilities', 'core/defer', 'core/log',
'core/notification', 'core/polyfill', 'core/requests', 'core/settings',
'core/site_config', 'core/storage', 'core/urls', 'core/user',
'core/utils', 'core/views', 'core/z', 'jquery', 'underscore'],
function(cache, capabilities, defer, log,
notification, polyfill, requests, settings,
siteConfig, storage, urls, user,
utils, views, z, $, _) {
var console = log('login');
var fxa_popup;
var retainPopup;
var pending_logins = [];
var packaged_origin = "app://" + window.location.host;
function oncancel() {
console.log('Login cancelled');
z.page.trigger('login_cancel');
_.invoke(pending_logins, 'reject');
pending_logins = [];
}
function signOutNotification() {
notification.notification({message: gettext('You have been signed out.')});
}
function signInNotification() {
notification.notification({message: gettext('You have been signed in.')});
}
function logOut() {
cache.flush_signed();
var isLoggedIn = false;
if (storage.getItem('user')) {
isLoggedIn = true;
}
user.clear_token();
z.body.removeClass('logged-in');
z.page.trigger('reload_chrome').trigger('before_logout');
if (!z.context.dont_reload_on_login) {
z.page.trigger('logged_out');
if (isLoggedIn) {
signOutNotification();
}
views.reload();
} else {
console.log('Reload on logout aborted by current view');
}
}
function logIn(data) {
var should_reload = !user.logged_in();
user.set_token(data.token, data.settings);
user.update_permissions(data.permissions);
user.update_apps(data.apps);
console.log('Login succeeded, preparing the app');
z.body.addClass('logged-in');
$('.loading-submit').removeClass('loading-submit');
z.page.trigger('reload_chrome').trigger('logged_in');
function resolve_pending() {
_.invoke(pending_logins, 'resolve');
pending_logins = [];
}
if (should_reload && !z.context.dont_reload_on_login) {
views.reload().done(function() {
resolve_pending();
signInNotification();
});
} else {
console.log('Reload on login aborted by current view');
}
}
function logInFailed(message) {
message = message || gettext('Sign in failed');
notification.notification({message: message});
$('.loading-submit').removeClass('loading-submit');
z.page.trigger('login_fail');
_.invoke(pending_logins, 'reject');
pending_logins = [];
}
z.body.on('click', '.persona', function(e) {
e.preventDefault();
var $this = $(this);
$('.loading-submit').removeClass('loading-submit');
$this.addClass('loading-submit');
startLogin({register: $this.hasClass('register')}).always(function() {
$this.removeClass('loading-submit').trigger('blur');
});
}).on('click', '.logout', utils._pd(function(e) {
requests.del(urls.api.url('logout'));
// Sync with Marketplace React/Redux projects.
var redux = localStorage.getItem('redux');
if (redux) {
redux = JSON.parse(redux);
if (redux.user && redux.user.token) {
redux.user.token = null;
localStorage.setItem('redux', JSON.stringify(redux));
}
}
if (capabilities.fallbackFxA()) {
logOut();
}
}));
function startLogin(options) {
var def = defer.Deferred();
pending_logins.push(def);
var opt = {register: false};
// Override our settings with the provided ones.
_.extend(opt, options);
if (capabilities.yulelogFxA()) {
window.top.postMessage({type: 'fxa-request'}, packaged_origin);
} else if (capabilities.fallbackFxA()) {
var fxa_url = fxa_redirect_url({register: opt.register});
if (opt.popupWindow) {
console.log('Changing location of supplied window to', fxa_url);
fxa_popup = opt.popupWindow;
fxa_popup.location = fxa_url;
retainPopup = true;
} else {
console.log('Opening new login window');
fxa_popup = utils.openWindow({url: fxa_url});
retainPopup = false;
// The same-origin policy prevents us from listening to events to
// know when the cross-origin FxA popup was closed. And we can't
// listen to `focus` on the main window because, unlike Chrome,
// Firefox does not fire `focus` when a popup is closed (presumably
// because the page never truly lost focus).
var popup_interval = setInterval(function() {
if (!fxa_popup || fxa_popup.closed) {
// The oncancel was cancelling prematurely when window is closed,
// prevents review dialog from popping up on login success.
// oncancel();
$('.loading-submit').removeClass('loading-submit').trigger('blur');
clearInterval(popup_interval);
}
}, 500);
}
} else {
console.log('Requesting login from Native FxA');
navigator.mozId.request({oncancel: oncancel});
}
return def.promise();
}
function gotVerifiedEmail(assertion) {
console.log('Got assertion from FxA');
var aud;
if (capabilities.yulelogFxA()) {
aud = packaged_origin;
} else {
aud = window.location.origin;
}
var data = {
assertion: assertion,
audience: aud,
};
z.page.trigger('before_login');
requests.post(urls.api.url('login'), data)
.done(logIn)
.fail(function(jqXHR, textStatus, error) {
console.warn('Assertion verification failed!', textStatus, error);
var err = jqXHR.responseText;
// Catch-all for XHR errors otherwise we'll trigger a notification
// with its message as one of the error templates.
if (jqXHR.status != 200) {
err = gettext('FxA login failed. A server error was encountered.');
}
logInFailed(err);
});
}
function startNativeFxA() {
var email = user.get_setting('email') || '';
if (email) {
console.log('Detected user', email);
} else {
console.log('No previous user detected');
}
console.log('Calling navigator.mozId.watch');
navigator.mozId.watch({
wantIssuer: 'firefox-accounts',
loggedInUser: email,
// This lets us change the cursor for the "Sign in" link.
onready: function() {z.body.addClass('persona-loaded');},
onlogin: gotVerifiedEmail,
onlogout: function() {
z.body.removeClass('logged-in');
z.page.trigger('reload_chrome').trigger('logout');
}
});
}
function registerFxAPostMessageHandler() {
window.addEventListener('message', function (msg) {
var valid_origins = [settings.api_url, window.location.origin];
if (msg.data && msg.data.auth_code &&
valid_origins.indexOf(msg.origin) !== -1) {
handle_fxa_login(msg.data.auth_code);
}
});
}
siteConfig.promise.done(function(data) {
if (capabilities.yulelogFxA()) {
console.log("setting up yulelog-fxa");
window.addEventListener('message', function (msg) {
if (!msg.data || !msg.data.type || msg.origin !== packaged_origin) {
return;
}
console.log("fxa message " + JSON.stringify(msg.data));
if (msg.data.type === 'fxa-login') {
gotVerifiedEmail(msg.data.assertion);
} else if (msg.data.type === 'fxa-logout') {
logOut();
} else if (msg.data.type === 'fxa-cancel') {
oncancel();
}
});
window.top.postMessage({type: 'fxa-watch',
email: user.get_setting('email') || ''},
packaged_origin);
} else if (!capabilities.fallbackFxA()) {
startNativeFxA();
} else {
// This lets us change the cursor for the "Sign in" link.
z.body.addClass('persona-loaded');
// Register the "message" handler here to avoid it being registered
// multiple times.
registerFxAPostMessageHandler();
}
});
var fxa_auth_url_key = 'fxa_auth_url';
function save_fxa_auth_url(url) {
storage.setItem(fxa_auth_url_key, url);
}
function get_fxa_auth_url() {
return storage.getItem(fxa_auth_url_key);
}
function handle_fxa_login(auth_code, state) {
var loginData = {
'auth_response': auth_code,
'state': state || settings.fxa_auth_state,
};
// Check for a host secific client_id override. See site_config.js for
// more info.
var clientIdOverride = siteConfig.fxa_client_id_for_origin();
if (clientIdOverride) {
loginData.client_id = clientIdOverride;
}
z.page.trigger('before_login');
requests.post(urls.api.url('fxa-login'), loginData)
.done(logIn)
.fail(function(jqXHR, textStatus, error) {
console.warn('FxA login failed', jqXHR, textStatus, error);
logInFailed();
}).always(function() {
if (fxa_popup && !retainPopup) {
console.log('Closing login popup');
fxa_popup.close();
} else if (fxa_popup) {
console.log('Keeping window around for re-use');
}
});
}
function fxa_redirect_url(opt) {
opt = opt || {};
var action = opt.register ? 'signup' : 'signin';
return utils.urlparams(settings.fxa_auth_url, {action: action});
}
return {
login: startLogin,
fxa_redirect_url: fxa_redirect_url,
get_fxa_auth_url: get_fxa_auth_url,
handle_fxa_login: handle_fxa_login,
};
});