This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
sign_in.js
143 lines (121 loc) · 3.92 KB
/
sign_in.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
define([
'underscore',
'lib/promise',
'views/base',
'views/form',
'stache!templates/sign_in',
'lib/constants',
'lib/session',
'views/mixins/password-mixin',
'lib/auth-errors',
'lib/validate',
'views/mixins/service-mixin'
],
function (_, p, BaseView, FormView, SignInTemplate, Constants, Session, PasswordMixin, AuthErrors, Validate, ServiceMixin) {
var t = BaseView.t;
var View = FormView.extend({
template: SignInTemplate,
className: 'sign-in',
initialize: function (options) {
options = options || {};
// reset any force auth status.
Session.set('forceAuth', false);
this.service = Session.service;
},
context: function () {
// Session.prefillEmail comes first because users can edit the email,
// go to another screen, edit the email again, and come back here. We
// want the last used email.
var email = Session.prefillEmail || this.searchParam('email');
return {
service: this.service,
serviceName: this.serviceName,
email: email,
password: Session.prefillPassword,
isSync: this.isSync(),
error: this.error
};
},
events: {
'change .show-password': 'onPasswordVisibilityChange',
'click a[href="/reset_password"]': 'resetPasswordIfKnownValidEmail'
},
beforeDestroy: function () {
Session.set('prefillEmail', this.$('.email').val());
Session.set('prefillPassword', this.$('.password').val());
},
beforeRender: function() {
var self = this;
return p().then(function () {
return FormView.prototype.beforeRender.call(self);
})
.then(function () {
if (self.hasService() && self.isSync()) {
return self.setServiceInfo();
}
});
},
submit: function () {
var email = this.$('.email').val();
var password = this.$('.password').val();
return this._signIn(email, password);
},
_signIn: function (email, password) {
var self = this;
return this.fxaClient.signIn(email, password)
.then(function (accountData) {
if (accountData.verified) {
return self.onSignInSuccess();
} else {
return self.onSignInUnverified();
}
})
.then(null, function (err) {
if (AuthErrors.is(err, 'UNKNOWN_ACCOUNT')) {
return self._suggestSignUp(err);
} else if (AuthErrors.is(err, 'USER_CANCELED_LOGIN')) {
self.logEvent('login:canceled');
// if user canceled login, just stop
return;
}
// re-throw error, it will be handled at a lower level.
throw err;
});
},
onSignInSuccess: function() {
// Don't switch to settings if we're trying to log in to
// Firefox. Firefox will show its own landing page
if (Session.get('context') !== Constants.FX_DESKTOP_CONTEXT) {
this.navigate('settings');
}
return true;
},
onSignInUnverified: function() {
var self = this;
return self.fxaClient.signUpResend()
.then(function () {
self.navigate('confirm');
});
},
onPasswordResetNavigate: function () {
this.navigate('reset_password');
},
_suggestSignUp: function (err) {
err.forceMessage = t('Unknown account. <a href="/signup">Sign up</a>');
return this.displayErrorUnsafe(err);
},
resetPasswordIfKnownValidEmail: BaseView.preventDefaultThen(function () {
var self = this;
return p().then(function () {
self.onPasswordResetNavigate();
});
})
});
_.extend(View.prototype, PasswordMixin);
_.extend(View.prototype, ServiceMixin);
return View;
});