@cocreate/users 1.9.13 → 1.10.1
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 +26 -0
- package/demo/login.html +1 -1
- package/demo/logout.html +1 -1
- package/demo/signup.html +1 -1
- package/package.json +6 -6
- package/src/client.js +39 -39
- package/src/server.js +42 -60
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
## [1.10.1](https://github.com/CoCreate-app/CoCreate-users/compare/v1.10.0...v1.10.1) (2022-11-21)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* bump d@cocreate ependencies ([ee0e697](https://github.com/CoCreate-app/CoCreate-users/commit/ee0e6973ba07012fba07db4ba0d97e31df1be136))
|
|
7
|
+
|
|
8
|
+
# [1.10.0](https://github.com/CoCreate-app/CoCreate-users/compare/v1.9.13...v1.10.0) (2022-11-21)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* bump cdn to 1.28.7 ([a80ebcc](https://github.com/CoCreate-app/CoCreate-users/commit/a80ebcc52c8f82e878241f0dae933039163c5207))
|
|
14
|
+
* bump cdn to 1.28.8 ([a66d8bd](https://github.com/CoCreate-app/CoCreate-users/commit/a66d8bd75045a968755362c05b323768ebee6e16))
|
|
15
|
+
* renamed data.data to data.document ([6aa2257](https://github.com/CoCreate-app/CoCreate-users/commit/6aa2257f3c755fa8cf112f89fe43c8270aa6c729))
|
|
16
|
+
* renamed getSocket to getSockets ([56fd7c9](https://github.com/CoCreate-app/CoCreate-users/commit/56fd7c90f68785e9f654e052bae9a7df5335e2f7))
|
|
17
|
+
* typo data | request ([2ef1f2f](https://github.com/CoCreate-app/CoCreate-users/commit/2ef1f2fb485d3e42159479e5d517476ea3785689))
|
|
18
|
+
* update crud functions to receive an array of objects as the response ([117db56](https://github.com/CoCreate-app/CoCreate-users/commit/117db56540891728946b183c561df56ad5aa23c0))
|
|
19
|
+
* update status to userStatus ([febb7ac](https://github.com/CoCreate-app/CoCreate-users/commit/febb7acb7fa13db900896db069195796a25064b2))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Features
|
|
23
|
+
|
|
24
|
+
* Add @cocreate/element-prototype ([9c2e8be](https://github.com/CoCreate-app/CoCreate-users/commit/9c2e8be1f7516877ee36074b0dcd1d9a03e3bd4a))
|
|
25
|
+
* updated to use new crud which has the abilty to use multiple databases ([88db3d3](https://github.com/CoCreate-app/CoCreate-users/commit/88db3d328bdc82d97c8e265bfb2e39c10d7383a5))
|
|
26
|
+
|
|
1
27
|
## [1.9.13](https://github.com/CoCreate-app/CoCreate-users/compare/v1.9.12...v1.9.13) (2022-10-02)
|
|
2
28
|
|
|
3
29
|
|
package/demo/login.html
CHANGED
package/demo/logout.html
CHANGED
package/demo/signup.html
CHANGED
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
host: 'server.cocreate.app' // Points to socket server. If not defined it will use the url in web browser by default
|
|
104
104
|
}
|
|
105
105
|
</script>
|
|
106
|
-
<script src="https://cdn.cocreate.app/1.28.
|
|
106
|
+
<script src="https://cdn.cocreate.app/1.28.8/CoCreate.min.js"></script>
|
|
107
107
|
<!-- <script src="../../../CoCreateJS/dist/CoCreate.js"></script> -->
|
|
108
108
|
|
|
109
109
|
</body>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/users",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.1",
|
|
4
4
|
"description": "A simple users component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"users",
|
|
@@ -61,11 +61,11 @@
|
|
|
61
61
|
"webpack-log": "^3.0.1"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@cocreate/actions": "^1.5.
|
|
65
|
-
"@cocreate/crud-client": "^1.
|
|
66
|
-
"@cocreate/docs": "^1.3.
|
|
67
|
-
"@cocreate/
|
|
68
|
-
"@cocreate/render": "^1.
|
|
64
|
+
"@cocreate/actions": "^1.5.13",
|
|
65
|
+
"@cocreate/crud-client": "^1.13.0",
|
|
66
|
+
"@cocreate/docs": "^1.3.20",
|
|
67
|
+
"@cocreate/element-prototype": "^1.0.0",
|
|
68
|
+
"@cocreate/render": "^1.16.1",
|
|
69
69
|
"mongodb": "^4.4.0"
|
|
70
70
|
}
|
|
71
71
|
}
|
package/src/client.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
/*globals CustomEvent, btoa*/
|
|
2
|
-
import
|
|
2
|
+
import CRUD from '@cocreate/crud-client';
|
|
3
3
|
import action from '@cocreate/actions';
|
|
4
4
|
import render from '@cocreate/render';
|
|
5
|
+
import '@cocreate/element-prototype';
|
|
6
|
+
|
|
7
|
+
let crud
|
|
8
|
+
if(CRUD && CRUD.default)
|
|
9
|
+
crud = CRUD.default
|
|
10
|
+
else
|
|
11
|
+
crud = CRUD
|
|
5
12
|
|
|
6
13
|
const CONST_PERMISSION_CLASS = 'checkPermission';
|
|
7
14
|
|
|
@@ -16,7 +23,7 @@ const CoCreateUser = {
|
|
|
16
23
|
initSocket: function() {
|
|
17
24
|
const self = this;
|
|
18
25
|
crud.listen('createUser', function(data) {
|
|
19
|
-
self.setDocumentId('users', data.
|
|
26
|
+
self.setDocumentId('users', data.document[0]._id);
|
|
20
27
|
document.dispatchEvent(new CustomEvent('createUser', {
|
|
21
28
|
detail: data
|
|
22
29
|
}));
|
|
@@ -46,37 +53,31 @@ const CoCreateUser = {
|
|
|
46
53
|
|
|
47
54
|
let request = {
|
|
48
55
|
collection,
|
|
56
|
+
document: {
|
|
57
|
+
lastLogin: new Date().toISOString(),
|
|
58
|
+
current_org: crud.socket.config.organization_id
|
|
59
|
+
},
|
|
49
60
|
filter: {
|
|
50
61
|
query
|
|
51
62
|
}
|
|
52
63
|
}
|
|
53
64
|
|
|
54
|
-
const socket = crud.socket.
|
|
55
|
-
if (!socket || !socket.connected || window && !window.navigator.onLine) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
response
|
|
65
|
-
|
|
66
|
-
response['success'] = true
|
|
67
|
-
response['status'] = "success"
|
|
68
|
-
this.loginResponse(response)
|
|
69
|
-
} else {
|
|
70
|
-
this.loginResponse(response)
|
|
71
|
-
}
|
|
72
|
-
})
|
|
65
|
+
const socket = crud.socket.getSockets()
|
|
66
|
+
if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine) {
|
|
67
|
+
crud.updateDocument(request).then((response) => {
|
|
68
|
+
response['success'] = false
|
|
69
|
+
response['status'] = "Login failed"
|
|
70
|
+
if (response.document) {
|
|
71
|
+
response['success'] = true
|
|
72
|
+
response['status'] = "success"
|
|
73
|
+
this.loginResponse(response)
|
|
74
|
+
} else {
|
|
75
|
+
this.loginResponse(response)
|
|
76
|
+
}
|
|
73
77
|
})
|
|
74
78
|
} else {
|
|
75
79
|
// ToDo: can be depreciated if we have another means of token generation
|
|
76
|
-
crud.send('login',
|
|
77
|
-
"collection": collection,
|
|
78
|
-
"loginData": query
|
|
79
|
-
});
|
|
80
|
+
crud.socket.send('login', request);
|
|
80
81
|
}
|
|
81
82
|
},
|
|
82
83
|
|
|
@@ -87,7 +88,7 @@ const CoCreateUser = {
|
|
|
87
88
|
window.localStorage.setItem('organization_id', crud.socket.config.organization_id);
|
|
88
89
|
window.localStorage.setItem("apiKey", crud.socket.config.apiKey);
|
|
89
90
|
window.localStorage.setItem("host", crud.socket.config.host);
|
|
90
|
-
window.localStorage.setItem('user_id', data.
|
|
91
|
+
window.localStorage.setItem('user_id', data.document[0]['_id']);
|
|
91
92
|
window.localStorage.setItem("token", token);
|
|
92
93
|
document.cookie = `token=${token};path=/`;
|
|
93
94
|
message = "Succesful Login";
|
|
@@ -142,13 +143,12 @@ const CoCreateUser = {
|
|
|
142
143
|
// ToDo: can get selected value from event/element, readDocument not required.
|
|
143
144
|
crud.readDocument({
|
|
144
145
|
collection: collection || 'users',
|
|
145
|
-
|
|
146
|
-
data: {
|
|
146
|
+
document: {
|
|
147
147
|
_id: user_id
|
|
148
148
|
},
|
|
149
149
|
}).then((data) => {
|
|
150
150
|
window.localStorage.setItem('apiKey', data['apiKey']);
|
|
151
|
-
window.localStorage.setItem('organization_id', data.
|
|
151
|
+
window.localStorage.setItem('organization_id', data.document[0]['current_org']);
|
|
152
152
|
window.localStorage.setItem('host', crud.socket.config.host);
|
|
153
153
|
|
|
154
154
|
document.dispatchEvent(new CustomEvent('logIn'));
|
|
@@ -231,7 +231,7 @@ const CoCreateUser = {
|
|
|
231
231
|
let statusEls = document.querySelectorAll(`[user-status][document_id='${data['user_id']}']`);
|
|
232
232
|
|
|
233
233
|
statusEls.forEach((el) => {
|
|
234
|
-
el.setAttribute('user-status', data['
|
|
234
|
+
el.setAttribute('user-status', data['userStatus']);
|
|
235
235
|
});
|
|
236
236
|
},
|
|
237
237
|
|
|
@@ -262,7 +262,7 @@ const CoCreateUser = {
|
|
|
262
262
|
else
|
|
263
263
|
org_id = crud.socket.config.organization_id;
|
|
264
264
|
|
|
265
|
-
let data = {
|
|
265
|
+
let data = {document: {}};
|
|
266
266
|
//. get form data
|
|
267
267
|
elements.forEach(el => {
|
|
268
268
|
let name = el.getAttribute('name');
|
|
@@ -272,20 +272,20 @@ const CoCreateUser = {
|
|
|
272
272
|
if (el.getAttribute('data-type') == 'array') {
|
|
273
273
|
value = [value];
|
|
274
274
|
}
|
|
275
|
-
data.
|
|
275
|
+
data.document[name] = value;
|
|
276
276
|
});
|
|
277
277
|
data['collection'] = 'users'
|
|
278
|
-
data.
|
|
279
|
-
data.
|
|
278
|
+
data.document['current_org'] = org_id;
|
|
279
|
+
data.document['connected_orgs'] = [org_id];
|
|
280
280
|
|
|
281
|
-
const socket = crud.socket.
|
|
282
|
-
if (!socket || !socket.connected || window && !window.navigator.onLine) {
|
|
281
|
+
const socket = crud.socket.getSockets()
|
|
282
|
+
if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine) {
|
|
283
283
|
// ToDo: can use updateDocument with filter query
|
|
284
284
|
crud.createDocument(data).then((response) => {
|
|
285
285
|
self.setDocumentId('users', response.document_id);
|
|
286
286
|
data.database = org_id
|
|
287
287
|
data.document_id = response.document_id
|
|
288
|
-
data.
|
|
288
|
+
data.document['_id'] = response.document_id
|
|
289
289
|
data.organization_id = org_id
|
|
290
290
|
crud.createDocument(request).then((response) => {
|
|
291
291
|
|
|
@@ -297,9 +297,9 @@ const CoCreateUser = {
|
|
|
297
297
|
})
|
|
298
298
|
} else {
|
|
299
299
|
// ToDo: creates user in platformdb
|
|
300
|
-
crud.send('createUser', {
|
|
300
|
+
crud.socket.send('createUser', {
|
|
301
301
|
collection: 'users',
|
|
302
|
-
data
|
|
302
|
+
...data,
|
|
303
303
|
orgDB: org_id
|
|
304
304
|
});
|
|
305
305
|
}
|
package/src/server.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
const {ObjectId} = require("mongodb");
|
|
2
|
-
|
|
3
1
|
class CoCreateUser {
|
|
4
|
-
constructor(wsManager,
|
|
2
|
+
constructor(wsManager, crud) {
|
|
5
3
|
this.wsManager = wsManager
|
|
6
|
-
this.
|
|
4
|
+
this.crud = crud
|
|
7
5
|
this.init()
|
|
8
6
|
}
|
|
9
7
|
|
|
@@ -17,31 +15,32 @@ class CoCreateUser {
|
|
|
17
15
|
|
|
18
16
|
async createUser(socket, data) {
|
|
19
17
|
const self = this;
|
|
20
|
-
if(!data.
|
|
18
|
+
if (!data.document) return;
|
|
21
19
|
|
|
22
|
-
try{
|
|
23
|
-
const collection = this.dbClient.db(data['organization_id']).collection(data['collection']);
|
|
20
|
+
try {
|
|
24
21
|
// Create new user in config db users collection
|
|
25
|
-
|
|
26
|
-
if(
|
|
22
|
+
this.crud.createDocument(data).then((data) => {
|
|
23
|
+
if (data.document[0] && data.document[0]._id) {
|
|
27
24
|
const orgDB = data.orgDB;
|
|
28
|
-
|
|
25
|
+
|
|
29
26
|
// if new orgDb Create new user in new org db users collection
|
|
30
27
|
if (orgDB && orgDB != data.organization_id) {
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
let Data = {...data, organization_id: orgDB}
|
|
29
|
+
self.crud.createDocument(Data)
|
|
33
30
|
}
|
|
34
|
-
|
|
35
|
-
self.wsManager.send(socket, 'createUser',
|
|
31
|
+
|
|
32
|
+
self.wsManager.send(socket, 'createUser', data);
|
|
36
33
|
|
|
37
34
|
// add new user to platformDB
|
|
38
35
|
if (data.organization_id != process.env.organization_id) {
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
let Data = {...data, organization_id: process.env.organization_id}
|
|
37
|
+
self.crud.createDocument(Data)
|
|
41
38
|
}
|
|
42
39
|
}
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
|
|
41
|
+
// self.wsManager.broadcast(socket, 'updateUserStatus', data)
|
|
42
|
+
})
|
|
43
|
+
} catch(error) {
|
|
45
44
|
console.log('createDocument error', error);
|
|
46
45
|
}
|
|
47
46
|
}
|
|
@@ -61,55 +60,43 @@ class CoCreateUser {
|
|
|
61
60
|
async login(socket, data) {
|
|
62
61
|
const self = this;
|
|
63
62
|
try {
|
|
64
|
-
|
|
65
|
-
const selectedDB = organization_id;
|
|
66
|
-
const collection = self.dbClient.db(selectedDB).collection(data["collection"]);
|
|
67
|
-
const query = new Object();
|
|
68
|
-
|
|
69
|
-
for (let item of data['loginData']) {
|
|
70
|
-
query[item.name] = item.value;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
collection.findOneAndUpdate(query, {$set: {lastLogin: new Date()}}, async function(error, result) {
|
|
63
|
+
this.crud.updateDocument(data).then(async (data) => {
|
|
74
64
|
let response = {
|
|
65
|
+
...data,
|
|
75
66
|
success: false,
|
|
76
67
|
message: "Login failed",
|
|
77
|
-
status: "failed"
|
|
78
|
-
uid: data['uid']
|
|
68
|
+
status: "failed"
|
|
79
69
|
}
|
|
80
|
-
|
|
70
|
+
|
|
71
|
+
if (data.document[0] && data.document[0]._id) {
|
|
81
72
|
let token = null;
|
|
82
73
|
if (self.wsManager.authInstance) {
|
|
83
|
-
token = await self.wsManager.authInstance.generateToken({user_id:
|
|
74
|
+
token = await self.wsManager.authInstance.generateToken({user_id: data.document[0]._id});
|
|
84
75
|
}
|
|
85
76
|
|
|
86
77
|
if (token && token != 'null')
|
|
87
78
|
response = { ...response,
|
|
88
79
|
success: true,
|
|
89
|
-
collection: data["collection"],
|
|
90
|
-
document_id: result.value['_id'],
|
|
91
|
-
data: {
|
|
92
|
-
_id: result.value['_id']
|
|
93
|
-
},
|
|
94
|
-
current_org: result.value['current_org'],
|
|
95
80
|
message: "Login successful",
|
|
96
81
|
status: "success",
|
|
82
|
+
userStatus: 'on',
|
|
97
83
|
token
|
|
98
84
|
};
|
|
99
85
|
|
|
100
|
-
let user_id = response.document_id;
|
|
101
|
-
const query = {
|
|
102
|
-
"_id": new ObjectId(user_id),
|
|
103
|
-
}
|
|
104
86
|
|
|
105
87
|
if (data.organization_id != process.env.organization_id) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
88
|
+
let Data = {organization_id: process.env.organization_id}
|
|
89
|
+
Data.document['_id'] = data.document[0]._id
|
|
90
|
+
Data.document['lastLogin'] = data.document[0].lastLogin
|
|
91
|
+
Data.document['organization_id'] = process.env.organization_id
|
|
92
|
+
crud.updateDocument(Data)
|
|
93
|
+
}
|
|
94
|
+
|
|
109
95
|
}
|
|
110
96
|
self.wsManager.send(socket, 'login', response)
|
|
111
|
-
|
|
112
|
-
})
|
|
97
|
+
self.wsManager.broadcast(socket, 'updateUserStatus', data)
|
|
98
|
+
})
|
|
99
|
+
|
|
113
100
|
} catch (error) {
|
|
114
101
|
console.log('login failed', error);
|
|
115
102
|
}
|
|
@@ -121,22 +108,17 @@ class CoCreateUser {
|
|
|
121
108
|
*/
|
|
122
109
|
async userStatus(socket, data) {
|
|
123
110
|
const self = this;
|
|
124
|
-
|
|
125
111
|
try {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const query = {
|
|
131
|
-
"_id": new ObjectId(data.user_id),
|
|
112
|
+
data.collection = 'users'
|
|
113
|
+
data['document'] = {
|
|
114
|
+
_id: data.user_id,
|
|
115
|
+
userStatus: data.userStatus
|
|
132
116
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
} else {
|
|
137
|
-
console.log('err', err)
|
|
138
|
-
}
|
|
117
|
+
|
|
118
|
+
this.crud.updateDocument(data).then((data) => {
|
|
119
|
+
self.wsManager.broadcast(socket, 'updateUserStatus', data)
|
|
139
120
|
})
|
|
121
|
+
|
|
140
122
|
} catch (error) {
|
|
141
123
|
console.log('userStatus error')
|
|
142
124
|
}
|