This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
complete_reset_password.js
157 lines (136 loc) · 4.9 KB
/
complete_reset_password.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
/* 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',
'views/base',
'views/form',
'stache!templates/complete_reset_password',
'lib/session',
'views/mixins/password-mixin',
'views/mixins/floating-placeholder-mixin',
'lib/validate',
'lib/auth-errors',
'views/mixins/service-mixin'
],
function (_, BaseView, FormView, Template, Session, PasswordMixin, FloatingPlaceholderMixin, Validate, AuthErrors, ServiceMixin) {
var View = FormView.extend({
template: Template,
className: 'complete_reset_password',
events: {
'change .show-password': 'onPasswordVisibilityChange',
'click #resend': BaseView.preventDefaultThen('resendResetEmail')
},
// beforeRender is asynchronous and returns a promise. Only render
// after beforeRender has finished its business.
beforeRender: function () {
try {
this.importSearchParam('token');
this.importSearchParam('code');
this.importSearchParam('email');
} catch(e) {
// This is an invalid link. Abort and show an error message
// before doing any more checks.
this.logEvent('complete_reset_password:link_damaged');
return true;
}
// Remove any spaces that are probably due to a MUA adding
// line breaks in the middle of the link.
this.token = this.token.replace(/ /g, '');
this.code = this.code.replace(/ /g, '');
if (! this._doesLinkValidate()) {
// One or more parameters fails validation. Abort and show an
// error message before doing any more checks.
this.logEvent('complete_reset_password:link_damaged');
return true;
}
var self = this;
return this.fxaClient.isPasswordResetComplete(this.token)
.then(function (isComplete) {
self._isLinkExpired = isComplete;
if (isComplete) {
self.logEvent('complete_reset_password:link_expired');
}
return true;
});
},
afterRender: function() {
this.initializePlaceholderFields();
},
_doesLinkValidate: function () {
return Validate.isTokenValid(this.token) &&
Validate.isCodeValid(this.code) &&
Validate.isEmailValid(this.email);
},
context: function () {
var doesLinkValidate = this._doesLinkValidate();
var isLinkExpired = this._isLinkExpired;
// damaged and expired links have special messages.
return {
isSync: this.isSync(),
isLinkDamaged: ! doesLinkValidate,
isLinkExpired: isLinkExpired,
isLinkValid: doesLinkValidate && ! isLinkExpired
};
},
isValidEnd: function () {
return this._getPassword() === this._getVPassword();
},
showValidationErrorsEnd: function () {
if (this._getPassword() !== this._getVPassword()) {
var err = AuthErrors.toError('PASSWORDS_DO_NOT_MATCH');
this.displayError(err);
}
},
submit: function () {
var password = this._getPassword();
var self = this;
return this.fxaClient.completePasswordReset(this.email, password, this.token, this.code)
.then(function () {
// Get a new sessionToken if we're in an OAuth flow
// so that we can generate FxA assertions
if (self.isOAuthSameBrowser()) {
// cache oauth params because signIn will clear them
var params = Session.oauth;
return self.fxaClient.signIn(self.email, password)
.then(function () {
// restore oauth params
Session.set('oauth', params);
});
}
})
.then(function () {
self.navigate('reset_password_complete');
}, function (err) {
if (AuthErrors.is(err, 'INVALID_TOKEN')) {
self.logError(err);
// The token has expired since the first check, re-render to
// show a screen that allows the user to receive a new link.
return self.render();
}
// all other errors are unexpected, bail.
throw err;
});
},
_getPassword: function () {
return this.$('#password').val();
},
_getVPassword: function () {
return this.$('#vpassword').val();
},
resendResetEmail: function () {
var self = this;
return this.fxaClient.passwordReset(this.email)
.then(function () {
self.navigate('confirm_reset_password');
}, function (err) {
self.displayError(err);
});
}
});
_.extend(View.prototype, PasswordMixin);
_.extend(View.prototype, ServiceMixin);
_.extend(View.prototype, FloatingPlaceholderMixin);
return View;
});