@cocreate/users 1.21.2 → 1.21.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/client.js +250 -250
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.21.3](https://github.com/CoCreate-app/CoCreate-users/compare/v1.21.2...v1.21.3) (2023-05-18)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* Refactored initialization of client and socket, signIn, signUp, signOut and updateUserStatus methods. ([cd98a9a](https://github.com/CoCreate-app/CoCreate-users/commit/cd98a9a30982ee61b31802c0879528d2d86c195d))
|
|
7
|
+
|
|
1
8
|
## [1.21.2](https://github.com/CoCreate-app/CoCreate-users/compare/v1.21.1...v1.21.2) (2023-05-12)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -8,266 +8,266 @@ import './index.css';
|
|
|
8
8
|
import localStorage from '@cocreate/local-storage';
|
|
9
9
|
|
|
10
10
|
const CoCreateUser = {
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
11
|
+
init: function () {
|
|
12
|
+
this.initSocket();
|
|
13
|
+
this.initSession();
|
|
14
|
+
this.initChangeOrg();
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
initSocket: function () {
|
|
18
|
+
const self = this;
|
|
19
|
+
crud.listen('updateUserStatus', (data) => self.updateUserStatus(data));
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
signIn: function (btn) {
|
|
23
|
+
let form = btn.closest('form');
|
|
24
|
+
let collection = form.getAttribute('collection');
|
|
25
|
+
let query = [];
|
|
26
|
+
|
|
27
|
+
const inputs = form.querySelectorAll('input[name="email"], input[name="password"], input[name="username"]');
|
|
28
|
+
|
|
29
|
+
inputs.forEach((input) => {
|
|
30
|
+
const name = input.getAttribute('name');
|
|
31
|
+
const value = input.getValue();
|
|
32
|
+
collection = input.getAttribute('collection') || collection;
|
|
33
|
+
query.push({ name, value, operator: '$eq' })
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
let request = {
|
|
37
|
+
collection,
|
|
38
|
+
document: {
|
|
39
|
+
lastSignIn: new Date().toISOString(),
|
|
40
|
+
current_org: crud.socket.config.organization_id
|
|
41
|
+
},
|
|
42
|
+
filter: {
|
|
43
|
+
query
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const socket = crud.socket.getSockets()
|
|
48
|
+
if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine || crud.socket.serverOrganization == false) {
|
|
49
|
+
crud.updateDocument(request).then((response) => {
|
|
50
|
+
response['success'] = false
|
|
51
|
+
response['status'] = "signIn failed"
|
|
52
|
+
if (response.document && response.document[0]) {
|
|
53
|
+
response['success'] = true
|
|
54
|
+
response['status'] = "success"
|
|
55
|
+
this.signInResponse(response)
|
|
56
|
+
} else {
|
|
57
|
+
this.signInResponse(response)
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
} else {
|
|
61
|
+
request.broadcastBrowser = false
|
|
62
|
+
crud.socket.send('signIn', request).then((response) => {
|
|
63
|
+
this.signInResponse(response)
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
signInResponse: function (data) {
|
|
69
|
+
let { success, status, message, token } = data;
|
|
70
|
+
|
|
71
|
+
if (success) {
|
|
72
|
+
localStorage.setItem('organization_id', crud.socket.config.organization_id);
|
|
73
|
+
localStorage.setItem("key", crud.socket.config.key);
|
|
74
|
+
localStorage.setItem("host", crud.socket.config.host);
|
|
75
|
+
localStorage.setItem('user_id', data.document[0]['_id']);
|
|
76
|
+
localStorage.setItem("token", token);
|
|
77
|
+
// document.cookie = `token=${token};path=/`;
|
|
78
|
+
message = "Succesful signIn";
|
|
79
|
+
document.dispatchEvent(new CustomEvent('signIn', {
|
|
80
|
+
detail: {}
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
else
|
|
84
|
+
message = "The email or password you entered is incorrect";
|
|
85
|
+
|
|
86
|
+
render.data({
|
|
87
|
+
selector: "[template='signIn']",
|
|
88
|
+
data: {
|
|
89
|
+
type: 'signIn',
|
|
90
|
+
status,
|
|
91
|
+
message,
|
|
92
|
+
success
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
signOut: (btn) => {
|
|
98
|
+
self = this;
|
|
99
|
+
localStorage.removeItem("user_id");
|
|
100
|
+
localStorage.removeItem("token");
|
|
101
|
+
|
|
102
|
+
// let allCookies = document.cookie.split(';');
|
|
103
|
+
|
|
104
|
+
// for (var i = 0; i < allCookies.length; i++)
|
|
105
|
+
// document.cookie = allCookies[i] + "=;expires=" +
|
|
106
|
+
// new Date(0).toUTCString();
|
|
107
|
+
|
|
108
|
+
render.data({
|
|
109
|
+
selector: "[template='signOut']",
|
|
110
|
+
data: {
|
|
111
|
+
type: 'signOut',
|
|
112
|
+
message: 'Succesfully logged out',
|
|
113
|
+
success: true
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// TODO: replace with Custom event system
|
|
118
|
+
document.dispatchEvent(new CustomEvent('signOut'));
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
signUp: function (btn) {
|
|
122
|
+
let formEl = btn.closest("form");
|
|
123
|
+
if (!formEl) return;
|
|
124
|
+
|
|
125
|
+
let organization_id = crud.socket.config.organization_id;
|
|
126
|
+
let data = form.getData(formEl, 'users')
|
|
127
|
+
data['collection'] = 'users'
|
|
128
|
+
// data.document['current_org'] = organization_id;
|
|
129
|
+
// data.document['connected_orgs'] = [organization_id];
|
|
130
|
+
data.organization_id = [organization_id];
|
|
131
|
+
|
|
132
|
+
// const socket = crud.socket.getSockets()
|
|
133
|
+
// if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine) {
|
|
134
|
+
// TODO: can use updateDocument with filter query
|
|
135
|
+
crud.createDocument(data).then((response) => {
|
|
136
|
+
form.setDocumentId(formEl, response)
|
|
137
|
+
|
|
138
|
+
render.data({
|
|
139
|
+
selector: "[template='signUp']",
|
|
140
|
+
data: {
|
|
141
|
+
type: 'signUp',
|
|
142
|
+
message: 'Succesfully Signed Up',
|
|
143
|
+
success: true
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
document.dispatchEvent(new CustomEvent('signUp', {
|
|
148
|
+
detail: response
|
|
149
|
+
}));
|
|
150
|
+
|
|
151
|
+
})
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
updateUserStatus: function (data) {
|
|
155
|
+
this.redirect(data)
|
|
156
|
+
if (data.user_id) {
|
|
157
|
+
let statusEls = document.querySelectorAll(`[user-status][document_id='${data['user_id']}']`);
|
|
158
|
+
|
|
159
|
+
statusEls.forEach((el) => {
|
|
160
|
+
el.setAttribute('user-status', data['userStatus']);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
redirect: (data) => {
|
|
167
|
+
if (data.user_id && data.user_id !== crud.socket.config.user_id)
|
|
168
|
+
return
|
|
169
|
+
if (!data.user_id && data.clientId !== crud.socket.clientId)
|
|
170
|
+
return
|
|
171
|
+
if (data.userStatus == 'on' || data.userStatus == 'idle') {
|
|
172
|
+
let redirectTag = document.querySelector('[session="true"]');
|
|
173
|
+
|
|
174
|
+
if (redirectTag) {
|
|
175
|
+
let redirectLink = redirectTag.getAttribute('href');
|
|
176
|
+
if (redirectLink) {
|
|
177
|
+
document.location.href = redirectLink;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
} else if (data.userStatus == 'off') {
|
|
181
|
+
let redirectTag = document.querySelector('[session="false"]');
|
|
182
|
+
|
|
183
|
+
if (redirectTag) {
|
|
184
|
+
let redirectLink = redirectTag.getAttribute('href');
|
|
185
|
+
if (redirectLink) {
|
|
186
|
+
localStorage.removeItem("user_id");
|
|
187
|
+
localStorage.removeItem("token");
|
|
188
|
+
|
|
189
|
+
// this.deleteCookie();
|
|
190
|
+
document.location.href = redirectLink;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
initSession: () => {
|
|
198
|
+
let redirectTag = document.querySelector('[session]');
|
|
199
|
+
|
|
200
|
+
if (redirectTag) {
|
|
201
|
+
crud.socket.send('sendMessage', {
|
|
202
|
+
message: 'checkSession',
|
|
203
|
+
broadcast: false,
|
|
204
|
+
broadcastSender: false,
|
|
205
|
+
broadcastBrowser: false
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
},
|
|
210
|
+
|
|
211
|
+
initChangeOrg: () => {
|
|
212
|
+
const user_id = localStorage.getItem('user_id');
|
|
213
|
+
|
|
214
|
+
if (!user_id) return;
|
|
215
|
+
|
|
216
|
+
let orgChangers = document.querySelectorAll('.org-changer');
|
|
217
|
+
|
|
218
|
+
for (let i = 0; i < orgChangers.length; i++) {
|
|
219
|
+
let orgChanger = orgChangers[i];
|
|
220
|
+
|
|
221
|
+
const collection = orgChanger.getAttribute('collection');
|
|
222
|
+
const id = orgChanger.getAttribute('document_id');
|
|
223
|
+
|
|
224
|
+
if (collection == 'users' && id == user_id) {
|
|
225
|
+
orgChanger.addEventListener('selected', function (e) {
|
|
226
|
+
// TODO: can get selected value from event/element, readDocument not required.
|
|
227
|
+
crud.readDocument({
|
|
228
|
+
collection: collection || 'users',
|
|
229
|
+
document: {
|
|
230
|
+
_id: user_id
|
|
231
|
+
},
|
|
232
|
+
}).then((data) => {
|
|
233
|
+
localStorage.setItem('key', data['key']);
|
|
234
|
+
localStorage.setItem('organization_id', data.document[0]['current_org']);
|
|
235
|
+
localStorage.setItem('host', crud.socket.config.host);
|
|
236
|
+
|
|
237
|
+
document.dispatchEvent(new CustomEvent('signIn'));
|
|
238
|
+
window.location.reload();
|
|
239
|
+
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
246
|
};
|
|
247
247
|
|
|
248
248
|
|
|
249
249
|
action.init({
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
250
|
+
name: "signUp",
|
|
251
|
+
endEvent: "signUp",
|
|
252
|
+
callback: (btn, data) => {
|
|
253
|
+
CoCreateUser.signUp(btn);
|
|
254
|
+
},
|
|
255
255
|
});
|
|
256
256
|
|
|
257
257
|
action.init({
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
258
|
+
name: "signIn",
|
|
259
|
+
endEvent: "signIn",
|
|
260
|
+
callback: (btn, data) => {
|
|
261
|
+
CoCreateUser.signIn(btn, data);
|
|
262
|
+
},
|
|
263
263
|
});
|
|
264
264
|
|
|
265
265
|
action.init({
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
266
|
+
name: "signOut",
|
|
267
|
+
endEvent: "signOut",
|
|
268
|
+
callback: (btn, data) => {
|
|
269
|
+
CoCreateUser.signOut(btn, data);
|
|
270
|
+
},
|
|
271
271
|
});
|
|
272
272
|
|
|
273
273
|
CoCreateUser.init();
|