This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
fxam_server_request.js
107 lines (93 loc) · 3.11 KB
/
fxam_server_request.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
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* global SettingsHelper, FxaModuleManager, MozActivity */
'use strict';
/*
* FxModuleServerRequest wraps the functionality exposed by the server,
* letting our code to be shielded against changes in the API of FxA.
*/
(function(exports) {
var pref = 'identity.fxaccounts.reset-password.url';
var fxaSettingsHelper = SettingsHelper(pref);
var fxaURL;
fxaSettingsHelper.get(function on_fxa_get_settings(url) {
fxaURL = url;
});
function _setAccountDetails(response) {
if(response && response.user && response.user.email) {
FxaModuleManager.setParam('done', true);
FxaModuleManager.setParam('verified', response.user.verified);
}
}
function _ensureFxaClient(callback) {
window.parent.LazyLoader.load('../js/fx_accounts_client.js', function() {
callback && callback();
});
}
var FxModuleServerRequest = {
checkEmail: function fxmsr_checkEmail(email, onsuccess, onerror) {
_ensureFxaClient(function() {
window.parent.FxAccountsClient.queryAccount(
email,
onsuccess,
onerror);
});
},
signIn: function fxmsr_signIn(email, password, onsuccess, onerror) {
function successHandler(response) {
_setAccountDetails(response);
var authenticated =
(response && response.user && response.user.verified) || false;
onsuccess && onsuccess({
authenticated: authenticated
});
}
function errorHandler(response) {
onerror && onerror(response);
}
_ensureFxaClient(function signIn() {
window.parent.FxAccountsClient.signIn(
email,
password,
successHandler,
errorHandler);
});
},
signUp: function fxmsr_signUp(email, password, onsuccess, onerror) {
function successHandler(response) {
_setAccountDetails(response);
onsuccess && onsuccess(response);
}
_ensureFxaClient(function signUp() {
window.parent.FxAccountsClient.signUp(
email,
password,
successHandler,
onerror);
});
},
requestPasswordReset:
function fxmsr_requestPasswordReset(email, onsuccess, onerror) {
var url = email ? fxaURL + '?email=' + email : fxaURL;
var activity = new MozActivity({
name: 'view',
data: {
type: 'url',
url: url
}
});
activity.onsuccess = function on_reset_success() {
// TODO When the browser loads, it is *behind* the system app. So we
// need to dismiss this app in order to let the user reset their
// password.
onsuccess && onsuccess();
FxaModuleManager.close('DIALOG_CLOSED_BY_USER');
};
activity.onerror = function on_reset_error(err) {
console.error(err);
onerror && onerror(err);
};
}
};
exports.FxModuleServerRequest = FxModuleServerRequest;
}(window));