@cocreate/users 1.7.29 → 1.9.0
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 +21 -0
- package/package.json +1 -1
- package/src/client.js +98 -81
- package/src/server.js +19 -82
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
# [1.9.0](https://github.com/CoCreate-app/CoCreate-users/compare/v1.8.0...v1.9.0) (2022-09-28)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* broadcast params reduced to socket, message, data ([9192a90](https://github.com/CoCreate-app/CoCreate-users/commit/9192a90024ed97a91061bf3af987d5427b64c7e7))
|
|
7
|
+
* remove createUserSocket ([6c6d7a2](https://github.com/CoCreate-app/CoCreate-users/commit/6c6d7a2818ec00fd29fdd8561c915758565c1003))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* config is now accessible from socket.config ([14157aa](https://github.com/CoCreate-app/CoCreate-users/commit/14157aae133a17561e624862dd3024a8ed1d0d68))
|
|
13
|
+
* create new user while offline ([4456e39](https://github.com/CoCreate-app/CoCreate-users/commit/4456e39d810e49a9479431b77b5e9b61445269c0))
|
|
14
|
+
|
|
15
|
+
# [1.8.0](https://github.com/CoCreate-app/CoCreate-users/compare/v1.7.29...v1.8.0) (2022-09-22)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* login works offline ([cd6a36b](https://github.com/CoCreate-app/CoCreate-users/commit/cd6a36ba14b8dd8e7ecc28c48cedfb9d23880653))
|
|
21
|
+
|
|
1
22
|
## [1.7.29](https://github.com/CoCreate-app/CoCreate-users/compare/v1.7.28...v1.7.29) (2022-09-01)
|
|
2
23
|
|
|
3
24
|
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/*globals CustomEvent, btoa*/
|
|
2
2
|
import crud from '@cocreate/crud-client';
|
|
3
|
-
// import input from '@cocreate/elements';
|
|
4
3
|
import action from '@cocreate/actions';
|
|
5
4
|
import render from '@cocreate/render';
|
|
6
5
|
|
|
@@ -8,22 +7,10 @@ const CONST_PERMISSION_CLASS = 'checkPermission';
|
|
|
8
7
|
|
|
9
8
|
const CoCreateUser = {
|
|
10
9
|
init: function() {
|
|
11
|
-
this.updatedCurrentOrg = false;
|
|
10
|
+
// this.updatedCurrentOrg = false;
|
|
12
11
|
this.initSocket();
|
|
13
12
|
this.initChangeOrg();
|
|
14
13
|
this.checkSession();
|
|
15
|
-
this.createUserSocket();
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
createUserSocket: function() {
|
|
19
|
-
var user_id = window.localStorage.getItem('user_id');
|
|
20
|
-
if (user_id) {
|
|
21
|
-
crud.socket.create({
|
|
22
|
-
namespace: 'users',
|
|
23
|
-
room: user_id,
|
|
24
|
-
host: window.config.host
|
|
25
|
-
})
|
|
26
|
-
}
|
|
27
14
|
},
|
|
28
15
|
|
|
29
16
|
initSocket: function() {
|
|
@@ -37,15 +24,13 @@ const CoCreateUser = {
|
|
|
37
24
|
crud.listen('fetchedUser', this.checkPermissions);
|
|
38
25
|
crud.listen('login', (instance) => self.loginResponse(instance));
|
|
39
26
|
crud.listen('updateUserStatus', this.updateUserStatus);
|
|
40
|
-
crud.listen('userCurrentOrg', (instance) => self.setCurrentOrg(instance));
|
|
41
27
|
},
|
|
42
28
|
|
|
43
29
|
loginRequest: function(btn) {
|
|
44
30
|
let form = btn.closest('form');
|
|
45
31
|
let collection = form.getAttribute('collection');
|
|
46
|
-
let
|
|
32
|
+
let query = [];
|
|
47
33
|
|
|
48
|
-
// const inputs = form.querySelectorAll('input, textarea');
|
|
49
34
|
const inputs = form.querySelectorAll('input[name="email"], input[name="password"], input[name="username"]');
|
|
50
35
|
|
|
51
36
|
inputs.forEach((input) => {
|
|
@@ -56,30 +41,55 @@ const CoCreateUser = {
|
|
|
56
41
|
}
|
|
57
42
|
collection = input.getAttribute('collection') || collection;
|
|
58
43
|
|
|
59
|
-
|
|
60
|
-
loginData[name] = value;
|
|
61
|
-
}
|
|
44
|
+
query.push({name, value, operator: '$eq'})
|
|
62
45
|
});
|
|
63
46
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
47
|
+
let request = {
|
|
48
|
+
collection,
|
|
49
|
+
filter: {
|
|
50
|
+
query
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const socket = crud.socket.getSocket()
|
|
55
|
+
if (!socket || !socket.connected || window && !window.navigator.onLine) {
|
|
56
|
+
|
|
57
|
+
// ToDo: can use updateDocument with filter query
|
|
58
|
+
crud.readDocuments(request).then((data) => {
|
|
59
|
+
data.data = data.data[0]
|
|
60
|
+
data.data['lastLogin'] = new Date().toISOString()
|
|
61
|
+
data.data['current_org'] = data.organization_id
|
|
62
|
+
crud.updateDocument(data).then((response) => {
|
|
63
|
+
response['success'] = false
|
|
64
|
+
response['status'] = "Login failed"
|
|
65
|
+
if (response.data) {
|
|
66
|
+
response['success'] = true
|
|
67
|
+
response['status'] = "success"
|
|
68
|
+
this.loginResponse(response)
|
|
69
|
+
} else {
|
|
70
|
+
this.loginResponse(response)
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
} else {
|
|
75
|
+
// ToDo: can be depreciated if we have another means of token generation
|
|
76
|
+
crud.send('login', {
|
|
77
|
+
"collection": collection,
|
|
78
|
+
"loginData": query
|
|
79
|
+
});
|
|
80
|
+
}
|
|
70
81
|
},
|
|
71
82
|
|
|
72
83
|
loginResponse: function(data) {
|
|
73
84
|
let { success, status, message, token } = data;
|
|
74
85
|
|
|
75
86
|
if (success) {
|
|
76
|
-
window.localStorage.setItem('organization_id',
|
|
77
|
-
window.localStorage.setItem("apiKey",
|
|
78
|
-
window.localStorage.setItem("host",
|
|
79
|
-
window.localStorage.setItem('user_id', data['
|
|
87
|
+
window.localStorage.setItem('organization_id', crud.socket.config.organization_id);
|
|
88
|
+
window.localStorage.setItem("apiKey", crud.socket.config.apiKey);
|
|
89
|
+
window.localStorage.setItem("host", crud.socket.config.host);
|
|
90
|
+
window.localStorage.setItem('user_id', data.data['_id']);
|
|
80
91
|
window.localStorage.setItem("token", token);
|
|
81
92
|
document.cookie = `token=${token};path=/`;
|
|
82
|
-
this.getCurrentOrg(data['document_id'], data['collection']);
|
|
83
93
|
message = "Succesful Login";
|
|
84
94
|
document.dispatchEvent(new CustomEvent('login', {
|
|
85
95
|
detail: {}
|
|
@@ -99,27 +109,10 @@ const CoCreateUser = {
|
|
|
99
109
|
});
|
|
100
110
|
},
|
|
101
111
|
|
|
102
|
-
getCurrentOrg: function(user_id, collection) {
|
|
103
|
-
crud.send('userCurrentOrg', {
|
|
104
|
-
"apiKey": window.config.apiKey,
|
|
105
|
-
"organization_id": window.config.organization_id,
|
|
106
|
-
"collection": collection || 'users',
|
|
107
|
-
"user_id": user_id,
|
|
108
|
-
});
|
|
109
|
-
},
|
|
110
|
-
|
|
111
|
-
setCurrentOrg: function(data) {
|
|
112
|
-
this.updatedCurrentOrg = true;
|
|
113
|
-
window.localStorage.setItem('apiKey', data['apiKey']);
|
|
114
|
-
window.localStorage.setItem('organization_id', data['current_org']);
|
|
115
|
-
window.localStorage.setItem('host', window.config.host);
|
|
116
|
-
|
|
117
|
-
document.dispatchEvent(new CustomEvent('logIn'));
|
|
118
|
-
},
|
|
119
|
-
|
|
120
112
|
logout: (btn) => {
|
|
121
113
|
self = this;
|
|
122
|
-
window.localStorage.
|
|
114
|
+
window.localStorage.removeItem("user_id");
|
|
115
|
+
window.localStorage.removeItem("token");
|
|
123
116
|
|
|
124
117
|
let allCookies = document.cookie.split(';');
|
|
125
118
|
|
|
@@ -141,23 +134,28 @@ const CoCreateUser = {
|
|
|
141
134
|
for (let i = 0; i < orgChangers.length; i++) {
|
|
142
135
|
let orgChanger = orgChangers[i];
|
|
143
136
|
|
|
144
|
-
const collection = orgChanger.getAttribute('collection')
|
|
137
|
+
const collection = orgChanger.getAttribute('collection');
|
|
145
138
|
const id = orgChanger.getAttribute('document_id');
|
|
146
139
|
|
|
147
140
|
if (collection == 'users' && id == user_id) {
|
|
148
|
-
orgChanger.addEventListener('
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
141
|
+
orgChanger.addEventListener('selected', function(e) {
|
|
142
|
+
// ToDo: can get selected value from event/element, readDocument not required.
|
|
143
|
+
crud.readDocument({
|
|
144
|
+
collection: collection || 'users',
|
|
145
|
+
document_id: user_id,
|
|
146
|
+
data: {
|
|
147
|
+
_id: user_id
|
|
148
|
+
},
|
|
149
|
+
}).then((data) => {
|
|
150
|
+
window.localStorage.setItem('apiKey', data['apiKey']);
|
|
151
|
+
window.localStorage.setItem('organization_id', data.data['current_org']);
|
|
152
|
+
window.localStorage.setItem('host', crud.socket.config.host);
|
|
153
|
+
|
|
154
|
+
document.dispatchEvent(new CustomEvent('logIn'));
|
|
155
|
+
window.location.reload();
|
|
156
|
+
|
|
157
|
+
})
|
|
158
|
+
|
|
161
159
|
});
|
|
162
160
|
}
|
|
163
161
|
}
|
|
@@ -182,7 +180,9 @@ const CoCreateUser = {
|
|
|
182
180
|
if (redirectTag) {
|
|
183
181
|
let redirectLink = redirectTag.getAttribute('href');
|
|
184
182
|
if (redirectLink) {
|
|
185
|
-
window.localStorage.
|
|
183
|
+
window.localStorage.removeItem("user_id");
|
|
184
|
+
window.localStorage.removeItem("token");
|
|
185
|
+
|
|
186
186
|
// this.deleteCookie();
|
|
187
187
|
document.location.href = redirectLink;
|
|
188
188
|
}
|
|
@@ -235,6 +235,7 @@ const CoCreateUser = {
|
|
|
235
235
|
});
|
|
236
236
|
},
|
|
237
237
|
|
|
238
|
+
// ToDo: variations exist in a few components
|
|
238
239
|
setDocumentId: function(collection, id) {
|
|
239
240
|
let orgIdElements = document.querySelectorAll(`[collection='${collection}']`);
|
|
240
241
|
if (orgIdElements && orgIdElements.length > 0) {
|
|
@@ -259,9 +260,9 @@ const CoCreateUser = {
|
|
|
259
260
|
if (orgIdElement)
|
|
260
261
|
org_id = orgIdElement.value;
|
|
261
262
|
else
|
|
262
|
-
org_id = config.organization_id;
|
|
263
|
+
org_id = crud.socket.config.organization_id;
|
|
263
264
|
|
|
264
|
-
let data = {};
|
|
265
|
+
let data = {data: {}};
|
|
265
266
|
//. get form data
|
|
266
267
|
elements.forEach(el => {
|
|
267
268
|
let name = el.getAttribute('name');
|
|
@@ -271,21 +272,37 @@ const CoCreateUser = {
|
|
|
271
272
|
if (el.getAttribute('data-type') == 'array') {
|
|
272
273
|
value = [value];
|
|
273
274
|
}
|
|
274
|
-
data[name] = value;
|
|
275
|
+
data.data[name] = value;
|
|
275
276
|
});
|
|
276
|
-
data['
|
|
277
|
-
data['
|
|
278
|
-
data['
|
|
279
|
-
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
277
|
+
data['collection'] = 'users'
|
|
278
|
+
data.data['current_org'] = org_id;
|
|
279
|
+
data.data['connected_orgs'] = [org_id];
|
|
280
|
+
|
|
281
|
+
const socket = crud.socket.getSocket()
|
|
282
|
+
if (!socket || !socket.connected || window && !window.navigator.onLine) {
|
|
283
|
+
// ToDo: can use updateDocument with filter query
|
|
284
|
+
crud.createDocument(data).then((response) => {
|
|
285
|
+
self.setDocumentId('users', response.document_id);
|
|
286
|
+
data.database = org_id
|
|
287
|
+
data.document_id = response.document_id
|
|
288
|
+
data.data['_id'] = response.document_id
|
|
289
|
+
data.organization_id = org_id
|
|
290
|
+
crud.createDocument(request).then((response) => {
|
|
291
|
+
|
|
292
|
+
document.dispatchEvent(new CustomEvent('createUser', {
|
|
293
|
+
detail: response
|
|
294
|
+
}));
|
|
295
|
+
|
|
296
|
+
})
|
|
297
|
+
})
|
|
298
|
+
} else {
|
|
299
|
+
// ToDo: creates user in platformdb
|
|
300
|
+
crud.send('createUser', {
|
|
301
|
+
collection: 'users',
|
|
302
|
+
data: data,
|
|
303
|
+
orgDB: org_id
|
|
304
|
+
});
|
|
305
|
+
}
|
|
289
306
|
},
|
|
290
307
|
};
|
|
291
308
|
|
package/src/server.js
CHANGED
|
@@ -9,14 +9,13 @@ class CoCreateUser {
|
|
|
9
9
|
|
|
10
10
|
init() {
|
|
11
11
|
if (this.wsManager) {
|
|
12
|
-
this.wsManager.on('createUser',
|
|
13
|
-
this.wsManager.on('login',
|
|
14
|
-
this.wsManager.on('
|
|
15
|
-
this.wsManager.on('userStatus', (socket, data, socketInfo) => this.userStatus(socket, data, socketInfo))
|
|
12
|
+
this.wsManager.on('createUser', (socket, data) => this.createUser(socket, data));
|
|
13
|
+
this.wsManager.on('login', (socket, data) => this.login(socket, data))
|
|
14
|
+
this.wsManager.on('userStatus', (socket, data) => this.userStatus(socket, data))
|
|
16
15
|
}
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
async createUser(socket, data
|
|
18
|
+
async createUser(socket, data) {
|
|
20
19
|
const self = this;
|
|
21
20
|
if(!data.data) return;
|
|
22
21
|
|
|
@@ -33,7 +32,7 @@ class CoCreateUser {
|
|
|
33
32
|
anotherCollection.insertOne({...data.data, organization_id : orgDB});
|
|
34
33
|
}
|
|
35
34
|
const response = { ...data, document_id: `${result.insertedId}`}
|
|
36
|
-
self.wsManager.send(socket, 'createUser', response
|
|
35
|
+
self.wsManager.send(socket, 'createUser', response);
|
|
37
36
|
|
|
38
37
|
// add new user to platformDB
|
|
39
38
|
if (data.organization_id != process.env.organization_id) {
|
|
@@ -59,7 +58,7 @@ class CoCreateUser {
|
|
|
59
58
|
organization_id: string
|
|
60
59
|
}
|
|
61
60
|
**/
|
|
62
|
-
async login(socket, data
|
|
61
|
+
async login(socket, data) {
|
|
63
62
|
const self = this;
|
|
64
63
|
try {
|
|
65
64
|
const {organization_id} = data
|
|
@@ -67,8 +66,8 @@ class CoCreateUser {
|
|
|
67
66
|
const collection = self.dbClient.db(selectedDB).collection(data["collection"]);
|
|
68
67
|
const query = new Object();
|
|
69
68
|
|
|
70
|
-
for (
|
|
71
|
-
query[
|
|
69
|
+
for (let item of data['loginData']) {
|
|
70
|
+
query[item.name] = item.value;
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
collection.findOneAndUpdate(query, {$set: {lastLogin: new Date()}}, async function(error, result) {
|
|
@@ -89,6 +88,9 @@ class CoCreateUser {
|
|
|
89
88
|
success: true,
|
|
90
89
|
collection: data["collection"],
|
|
91
90
|
document_id: result.value['_id'],
|
|
91
|
+
data: {
|
|
92
|
+
_id: result.value['_id']
|
|
93
|
+
},
|
|
92
94
|
current_org: result.value['current_org'],
|
|
93
95
|
message: "Login successful",
|
|
94
96
|
status: "success",
|
|
@@ -105,7 +107,7 @@ class CoCreateUser {
|
|
|
105
107
|
platformDB.updateOne(query, {$set: {lastLogin: new Date()}})
|
|
106
108
|
}
|
|
107
109
|
}
|
|
108
|
-
self.wsManager.send(socket, 'login', response
|
|
110
|
+
self.wsManager.send(socket, 'login', response)
|
|
109
111
|
console.log(`${response.message} user_id: ${result.value['_id']}`)
|
|
110
112
|
});
|
|
111
113
|
} catch (error) {
|
|
@@ -113,92 +115,27 @@ class CoCreateUser {
|
|
|
113
115
|
}
|
|
114
116
|
}
|
|
115
117
|
|
|
116
|
-
/**
|
|
117
|
-
data = {
|
|
118
|
-
namespace: string,
|
|
119
|
-
collection: string,
|
|
120
|
-
user_id: string,
|
|
121
|
-
href: string
|
|
122
|
-
}
|
|
123
|
-
**/
|
|
124
|
-
async userCurrentOrg(socket, data, socketInfo) {
|
|
125
|
-
try {
|
|
126
|
-
const self = this;
|
|
127
|
-
const {organization_id, db} = data
|
|
128
|
-
const selectedDB = db || organization_id;
|
|
129
|
-
const collection = this.dbClient.db(selectedDB).collection(data["collection"]);
|
|
130
|
-
|
|
131
|
-
let query = new Object();
|
|
132
|
-
|
|
133
|
-
query['_id'] = new ObjectId(data['user_id']);
|
|
134
|
-
|
|
135
|
-
collection.find(query).toArray(function(error, result) {
|
|
136
|
-
|
|
137
|
-
if (!error && result && result.length > 0) {
|
|
138
|
-
|
|
139
|
-
if (result.length > 0) {
|
|
140
|
-
let org_id = result[0]['current_org'];
|
|
141
|
-
const orgCollection = self.dbClient.db(selectedDB).collection('organizations');
|
|
142
|
-
|
|
143
|
-
orgCollection.find({"_id": new ObjectId(org_id),}).toArray(function(err, res) {
|
|
144
|
-
if (!err && res && res.length > 0) {
|
|
145
|
-
self.wsManager.send(socket, 'userCurrentOrg', {
|
|
146
|
-
success: true,
|
|
147
|
-
user_id: result[0]['_id'],
|
|
148
|
-
current_org: result[0]['current_org'],
|
|
149
|
-
apiKey: res[0]['apiKey'],
|
|
150
|
-
href: data['href'],
|
|
151
|
-
uid: data['uid']
|
|
152
|
-
}, socketInfo)
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
} else {
|
|
158
|
-
// socket.emit('loginResult', {
|
|
159
|
-
// success: false
|
|
160
|
-
// });
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
} catch (error) {
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
118
|
|
|
169
119
|
/**
|
|
170
120
|
* status: 'on/off/idle'
|
|
171
121
|
*/
|
|
172
|
-
async userStatus(socket, data
|
|
122
|
+
async userStatus(socket, data) {
|
|
173
123
|
const self = this;
|
|
174
|
-
const {info, status} = data;
|
|
175
|
-
|
|
176
|
-
const items = info.split('/');
|
|
177
|
-
|
|
178
|
-
if (items[0] !== 'users') {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (!items[1]) return;
|
|
183
124
|
|
|
184
125
|
try {
|
|
185
126
|
const {organization_id, db} = data
|
|
186
127
|
const selectedDB = db || organization_id;
|
|
187
128
|
const collection = self.dbClient.db(selectedDB).collection('users');
|
|
188
|
-
const user_id = items[
|
|
129
|
+
// const user_id = items[2];
|
|
189
130
|
const query = {
|
|
190
|
-
"_id": new ObjectId(user_id),
|
|
131
|
+
"_id": new ObjectId(data.user_id),
|
|
191
132
|
}
|
|
192
|
-
collection.updateOne(query, {$set: {status: status}}, function(err, res) {
|
|
133
|
+
collection.updateOne(query, {$set: {status: data.status}}, function(err, res) {
|
|
193
134
|
if (!err) {
|
|
194
|
-
self.wsManager.broadcast(socket, '
|
|
195
|
-
|
|
196
|
-
user_id,
|
|
197
|
-
status
|
|
198
|
-
}, socketInfo)
|
|
199
|
-
}
|
|
200
|
-
else
|
|
135
|
+
self.wsManager.broadcast(socket, 'updateUserStatus', data)
|
|
136
|
+
} else {
|
|
201
137
|
console.log('err', err)
|
|
138
|
+
}
|
|
202
139
|
})
|
|
203
140
|
} catch (error) {
|
|
204
141
|
console.log('userStatus error')
|