This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
change_password.js
103 lines (85 loc) · 2.93 KB
/
change_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
/* 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/change_password',
'lib/session',
'views/mixins/password-mixin',
'views/mixins/floating-placeholder-mixin',
'lib/auth-errors',
'views/mixins/service-mixin'
],
function (_, BaseView, FormView, Template, Session, PasswordMixin, FloatingPlaceholderMixin, AuthErrors, ServiceMixin) {
var t = BaseView.t;
var View = FormView.extend({
// user must be authenticated to change password
mustAuth: true,
template: Template,
className: 'change-password',
events: {
'click #back': 'back',
'keyup #back': 'backOnEnter',
'change .show-password': 'onPasswordVisibilityChange',
'click #resend': 'resendVerificationEmail'
},
context: function () {
return {
isSync: this.isSync()
};
},
afterRender: function() {
this.initializePlaceholderFields();
},
submit: function () {
var email = Session.email;
var oldPassword = this.$('#old_password').val();
var newPassword = this.$('#new_password').val();
this.hideError();
var self = this;
// Try to sign the user in before checking whether the
// passwords are the same. If the user typed the incorrect old
// password, they should know that first.
return this.fxaClient.checkPassword(email, oldPassword)
.then(function () {
if (oldPassword === newPassword) {
throw AuthErrors.toError('PASSWORDS_MUST_BE_DIFFERENT');
}
return self.fxaClient.changePassword(
email, oldPassword, newPassword);
})
.then(function () {
self.navigate('settings', {
success: t('Password changed')
});
}, function (err) {
if (AuthErrors.is(err, 'UNVERIFIED_ACCOUNT')) {
err.forceMessage = t('Unverified account. <a href="#" id="resend">Resend verification email</a>.');
return self.displayErrorUnsafe(err);
}
throw err;
});
},
resendVerificationEmail: BaseView.preventDefaultThen(function () {
var self = this;
return this.fxaClient.signUpResend()
.then(function () {
self.navigate('confirm');
}, function (err) {
if (AuthErrors.is(err, 'INVALID_TOKEN')) {
return self.navigate('signup', {
error: err
});
}
throw self.displayError(err);
});
})
});
_.extend(View.prototype, PasswordMixin);
_.extend(View.prototype, FloatingPlaceholderMixin);
_.extend(View.prototype, ServiceMixin);
return View;
});