/
authentication-popup.js
100 lines (82 loc) · 3.33 KB
/
authentication-popup.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
define([
'uiComponent',
'Magento_Ui/js/modal/modal',
'Magento_Customer/js/customer-data'
], function (Component, modal, customerData) {
'use strict';
var loginAction, loginCallbacks = [];
loginAction = function (loginData, redirectUrl, isGlobal, messageContainer) {
messageContainer = messageContainer || $.registry.first('uiMessages');
return $.post({
url: window.authenticationPopup.customerLoginUrl,
data: JSON.stringify(loginData),
global: isGlobal === undefined ? true : isGlobal,
done: (response) => {
if (response.errors) {
return messageContainer.addErrorMessage(response.message);
}
customerData.invalidate(['customer']);
if (response.redirectUrl || redirectUrl) {
window.location.href = response.redirectUrl || redirectUrl;
} else {
location.reload();
}
},
fail: () => messageContainer.addErrorMessage($t('Could not authenticate. Please try again later')),
always: () => loginCallbacks.forEach((callback) => callback(loginData))
});
};
loginAction.registerLoginCallback = (callback) => loginCallbacks.push(callback);
$.breezemap['Magento_Customer/js/action/login'] = loginAction;
$(document).on('breeze:destroy', () => (loginCallbacks = []) && true);
$.view('authPopup', {
component: 'Magento_Customer/js/view/authentication-popup',
defaults: {
template: 'Magento_Customer/authentication-popup',
registerUrl: window.authenticationPopup?.customerRegisterUrl,
forgotPasswordUrl: window.authenticationPopup?.customerForgotPasswordUrl,
autocomplete: window.authenticationPopup?.autocomplete,
isLoading: ko.observable(false)
},
create: function () {
loginAction.registerLoginCallback(() => this.isLoading(false));
},
_applyBindings: function (element, force) {
if (force) {
this._super(element);
}
},
isActive: () => !customerData.get('customer')(),
setModalElement: function (element) {
if (this.modalWindow) {
return;
}
this.modalWindow = element;
modal({
'modalClass': 'popup-authentication',
'focus': '[name=username]',
'trigger': '.proceed-to-checkout',
'buttons': []
}, $(this.modalWindow));
$(this.modalWindow).on('modal:opened', () => {
$(this.modalWindow).trigger('contentUpdated');
});
},
showModal: function () {
this._applyBindings(this.element[0], true);
$(this.modalWindow).modal('openModal');
},
login: function (component, event) {
var formElement = $(event.currentTarget),
loginData = formElement.serializeJSON();
if (formElement.data('prevent-submit')
|| !formElement.validation()
|| !formElement.validation('isValid')
) {
return false;
}
this.isLoading(true);
loginAction(loginData);
}
});
});