@cocreate/users 1.8.0 → 1.9.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 CHANGED
@@ -1,3 +1,24 @@
1
+ ## [1.9.1](https://github.com/CoCreate-app/CoCreate-users/compare/v1.9.0...v1.9.1) (2022-09-29)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * config renameed to CoCreateConfig ([b91eecb](https://github.com/CoCreate-app/CoCreate-users/commit/b91eecbbc1e518379557ec1021949b3af954a3f5))
7
+
8
+ # [1.9.0](https://github.com/CoCreate-app/CoCreate-users/compare/v1.8.0...v1.9.0) (2022-09-28)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * broadcast params reduced to socket, message, data ([9192a90](https://github.com/CoCreate-app/CoCreate-users/commit/9192a90024ed97a91061bf3af987d5427b64c7e7))
14
+ * remove createUserSocket ([6c6d7a2](https://github.com/CoCreate-app/CoCreate-users/commit/6c6d7a2818ec00fd29fdd8561c915758565c1003))
15
+
16
+
17
+ ### Features
18
+
19
+ * config is now accessible from socket.config ([14157aa](https://github.com/CoCreate-app/CoCreate-users/commit/14157aae133a17561e624862dd3024a8ed1d0d68))
20
+ * create new user while offline ([4456e39](https://github.com/CoCreate-app/CoCreate-users/commit/4456e39d810e49a9479431b77b5e9b61445269c0))
21
+
1
22
  # [1.8.0](https://github.com/CoCreate-app/CoCreate-users/compare/v1.7.29...v1.8.0) (2022-09-22)
2
23
 
3
24
 
package/demo/login.html CHANGED
@@ -76,7 +76,7 @@
76
76
  </div>
77
77
 
78
78
  <script>
79
- var config = {
79
+ var CoCreateConfig = {
80
80
  apiKey: '2061acef-0451-4545-f754-60cf8160', // Your apiKey can be retrived after signing up at https://cocreate.app
81
81
  organization_id: '5ff747727005da1c272740ab', // Your organization_id can be retrived after signing up at https://cocreate.app
82
82
  host: 'server.cocreate.app' // Points to socket server. If not defined it will use the url in web browser by default
package/demo/signup.html CHANGED
@@ -97,7 +97,7 @@
97
97
  </div>
98
98
 
99
99
  <script>
100
- var config = {
100
+ var CoCreateConfig = {
101
101
  apiKey: '2061acef-0451-4545-f754-60cf8160', // Your apiKey can be retrived after signing up at https://cocreate.app
102
102
  organization_id: '5ff747727005da1c272740ab', // Your organization_id can be retrived after signing up at https://cocreate.app
103
103
  host: 'server.cocreate.app' // Points to socket server. If not defined it will use the url in web browser by default
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/users",
3
- "version": "1.8.0",
3
+ "version": "1.9.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",
package/src/client.js CHANGED
@@ -7,22 +7,10 @@ const CONST_PERMISSION_CLASS = 'checkPermission';
7
7
 
8
8
  const CoCreateUser = {
9
9
  init: function() {
10
- this.updatedCurrentOrg = false;
10
+ // this.updatedCurrentOrg = false;
11
11
  this.initSocket();
12
12
  this.initChangeOrg();
13
13
  this.checkSession();
14
- this.createUserSocket();
15
- },
16
-
17
- createUserSocket: function() {
18
- var user_id = window.localStorage.getItem('user_id');
19
- if (user_id) {
20
- crud.socket.create({
21
- namespace: 'users',
22
- room: user_id,
23
- host: window.config.host
24
- })
25
- }
26
14
  },
27
15
 
28
16
  initSocket: function() {
@@ -36,7 +24,6 @@ const CoCreateUser = {
36
24
  crud.listen('fetchedUser', this.checkPermissions);
37
25
  crud.listen('login', (instance) => self.loginResponse(instance));
38
26
  crud.listen('updateUserStatus', this.updateUserStatus);
39
- crud.listen('userCurrentOrg', (instance) => self.setCurrentOrg(instance));
40
27
  },
41
28
 
42
29
  loginRequest: function(btn) {
@@ -64,13 +51,14 @@ const CoCreateUser = {
64
51
  }
65
52
  }
66
53
 
67
- const socket = crud.socket.getSocket({})
54
+ const socket = crud.socket.getSocket()
68
55
  if (!socket || !socket.connected || window && !window.navigator.onLine) {
69
-
56
+
57
+ // ToDo: can use updateDocument with filter query
70
58
  crud.readDocuments(request).then((data) => {
71
59
  data.data = data.data[0]
72
60
  data.data['lastLogin'] = new Date().toISOString()
73
-
61
+ data.data['current_org'] = data.organization_id
74
62
  crud.updateDocument(data).then((response) => {
75
63
  response['success'] = false
76
64
  response['status'] = "Login failed"
@@ -84,9 +72,8 @@ const CoCreateUser = {
84
72
  })
85
73
  })
86
74
  } else {
75
+ // ToDo: can be depreciated if we have another means of token generation
87
76
  crud.send('login', {
88
- "apiKey": window.config.apiKey,
89
- "organization_id": window.config.organization_id,
90
77
  "collection": collection,
91
78
  "loginData": query
92
79
  });
@@ -97,13 +84,12 @@ const CoCreateUser = {
97
84
  let { success, status, message, token } = data;
98
85
 
99
86
  if (success) {
100
- window.localStorage.setItem('organization_id', window.config.organization_id);
101
- window.localStorage.setItem("apiKey", window.config.apiKey);
102
- window.localStorage.setItem("host", window.config.host);
103
- window.localStorage.setItem('user_id', data['document_id']);
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']);
104
91
  window.localStorage.setItem("token", token);
105
92
  document.cookie = `token=${token};path=/`;
106
- this.getCurrentOrg(data['document_id'], data['collection']);
107
93
  message = "Succesful Login";
108
94
  document.dispatchEvent(new CustomEvent('login', {
109
95
  detail: {}
@@ -123,27 +109,10 @@ const CoCreateUser = {
123
109
  });
124
110
  },
125
111
 
126
- getCurrentOrg: function(user_id, collection) {
127
- crud.send('userCurrentOrg', {
128
- "apiKey": window.config.apiKey,
129
- "organization_id": window.config.organization_id,
130
- "collection": collection || 'users',
131
- "user_id": user_id,
132
- });
133
- },
134
-
135
- setCurrentOrg: function(data) {
136
- this.updatedCurrentOrg = true;
137
- window.localStorage.setItem('apiKey', data['apiKey']);
138
- window.localStorage.setItem('organization_id', data['current_org']);
139
- window.localStorage.setItem('host', window.config.host);
140
-
141
- document.dispatchEvent(new CustomEvent('logIn'));
142
- },
143
-
144
112
  logout: (btn) => {
145
113
  self = this;
146
- window.localStorage.clear();
114
+ window.localStorage.removeItem("user_id");
115
+ window.localStorage.removeItem("token");
147
116
 
148
117
  let allCookies = document.cookie.split(';');
149
118
 
@@ -165,23 +134,28 @@ const CoCreateUser = {
165
134
  for (let i = 0; i < orgChangers.length; i++) {
166
135
  let orgChanger = orgChangers[i];
167
136
 
168
- const collection = orgChanger.getAttribute('collection') ? orgChanger.getAttribute('collection') : 'module_activity';
137
+ const collection = orgChanger.getAttribute('collection');
169
138
  const id = orgChanger.getAttribute('document_id');
170
139
 
171
140
  if (collection == 'users' && id == user_id) {
172
- orgChanger.addEventListener('selectedValue', function(e) {
173
-
174
- setTimeout(function() {
175
- getCurrentOrg(user_id);
176
-
177
- var timer = setInterval(function() {
178
- if (updatedCurrentOrg) {
179
- window.location.reload();
180
-
181
- clearInterval(timer);
182
- }
183
- }, 100);
184
- }, 300);
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
+
185
159
  });
186
160
  }
187
161
  }
@@ -206,7 +180,9 @@ const CoCreateUser = {
206
180
  if (redirectTag) {
207
181
  let redirectLink = redirectTag.getAttribute('href');
208
182
  if (redirectLink) {
209
- window.localStorage.clear();
183
+ window.localStorage.removeItem("user_id");
184
+ window.localStorage.removeItem("token");
185
+
210
186
  // this.deleteCookie();
211
187
  document.location.href = redirectLink;
212
188
  }
@@ -259,6 +235,7 @@ const CoCreateUser = {
259
235
  });
260
236
  },
261
237
 
238
+ // ToDo: variations exist in a few components
262
239
  setDocumentId: function(collection, id) {
263
240
  let orgIdElements = document.querySelectorAll(`[collection='${collection}']`);
264
241
  if (orgIdElements && orgIdElements.length > 0) {
@@ -283,9 +260,9 @@ const CoCreateUser = {
283
260
  if (orgIdElement)
284
261
  org_id = orgIdElement.value;
285
262
  else
286
- org_id = config.organization_id;
263
+ org_id = crud.socket.config.organization_id;
287
264
 
288
- let data = {};
265
+ let data = {data: {}};
289
266
  //. get form data
290
267
  elements.forEach(el => {
291
268
  let name = el.getAttribute('name');
@@ -295,21 +272,37 @@ const CoCreateUser = {
295
272
  if (el.getAttribute('data-type') == 'array') {
296
273
  value = [value];
297
274
  }
298
- data[name] = value;
275
+ data.data[name] = value;
299
276
  });
300
- data['current_org'] = org_id;
301
- data['connected_orgs'] = [org_id];
302
- data['organization_id'] = config.organization_id;
303
-
304
- const room = config.organization_id;
305
-
306
- crud.send('createUser', {
307
- apiKey: config.apiKey,
308
- organization_id: config.organization_id,
309
- collection: 'users',
310
- data: data,
311
- orgDB: org_id
312
- }, room);
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
+ }
313
306
  },
314
307
  };
315
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', (socket, data, socketInfo) => this.createUser(socket, data, socketInfo));
13
- this.wsManager.on('login', (socket, data, socketInfo) => this.login(socket, data, socketInfo))
14
- this.wsManager.on('userCurrentOrg', (socket, data, socketInfo) => this.userCurrentOrg(socket, data, socketInfo))
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, socketInfo) {
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, socketInfo);
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, socketInfo) {
61
+ async login(socket, data) {
63
62
  const self = this;
64
63
  try {
65
64
  const {organization_id} = data
@@ -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, socketInfo)
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, socketInfo) {
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[1];
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, '', null, 'updateUserStatus',
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')