@cocreate/users 1.27.4 → 1.27.6

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,22 @@
1
+ ## [1.27.6](https://github.com/CoCreate-app/CoCreate-users/compare/v1.27.5...v1.27.6) (2023-10-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * data.socket not found ([0b0b1e3](https://github.com/CoCreate-app/CoCreate-users/commit/0b0b1e32546758ef0ec0782817b3d5c94c595ee5))
7
+ * declartion of render functions using object spread ([30f9242](https://github.com/CoCreate-app/CoCreate-users/commit/30f924235a66ce2be22a9f53d92e6425db59708e))
8
+ * missing await and organization_id ([df79036](https://github.com/CoCreate-app/CoCreate-users/commit/df79036e7ba786bf6698c2cad91c5bbd4f3e50b5))
9
+ * replaced message with socket ([5036966](https://github.com/CoCreate-app/CoCreate-users/commit/503696642d5df6c8fba76862fc59db1d010b3dea))
10
+ * signup ([bf716d8](https://github.com/CoCreate-app/CoCreate-users/commit/bf716d8d94cf16641e01cb3ca56f4ecbe41cebef))
11
+ * update to support socket ([3fdd085](https://github.com/CoCreate-app/CoCreate-users/commit/3fdd0853d83ade29069977570dcb9ad67bf17318))
12
+
13
+ ## [1.27.5](https://github.com/CoCreate-app/CoCreate-users/compare/v1.27.4...v1.27.5) (2023-10-17)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * await getValue() ([fc610fd](https://github.com/CoCreate-app/CoCreate-users/commit/fc610fd8739013a728edcfcabb790ad2fa242dbc))
19
+
1
20
  ## [1.27.4](https://github.com/CoCreate-app/CoCreate-users/compare/v1.27.3...v1.27.4) (2023-10-14)
2
21
 
3
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/users",
3
- "version": "1.27.4",
3
+ "version": "1.27.6",
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
@@ -1,10 +1,12 @@
1
1
  /*globals CustomEvent, btoa*/
2
2
  import Crud from '@cocreate/crud-client';
3
- import Action from '@cocreate/actions';
3
+ import Actions from '@cocreate/actions';
4
4
  import Elements from '@cocreate/elements';
5
- import Render from '@cocreate/render';
5
+ import { render } from '@cocreate/render';
6
6
  import '@cocreate/element-prototype';
7
7
  import './index.css';
8
+
9
+ // TODO: Replace with @cocreate/config
8
10
  import localStorage from '@cocreate/local-storage';
9
11
 
10
12
  const CoCreateUser = {
@@ -22,51 +24,43 @@ const CoCreateUser = {
22
24
  Crud.listen('updateUserStatus', (data) => self.updateUserStatus(data));
23
25
  },
24
26
 
25
- signUp: async function (btn) {
26
- let formEl = btn.closest("form");
27
- if (!formEl) return;
27
+ signUp: async function (action) {
28
+ if (!action.form) return;
28
29
 
29
- let array = formEl.getAttribute('array')
30
- if (!array) {
31
- for (let el of formEl) {
32
- array = el.getAttribute('array');
33
- if (array)
34
- break;
35
- }
36
- }
30
+ let data = await Elements.save(action.form)
31
+ let user = data[0]
32
+ // data.method = 'create.object'
33
+ // data['array'] = array
37
34
 
38
- let data = Elements.getFormData(formEl, array)
39
- data.method = 'create.object'
40
- data['array'] = array
41
- data.organization_id = organization_id;
35
+ // data.organization_id = organization_id;
42
36
 
43
- if (!data.object[0]._id)
44
- data.object[0]._id = Crud.ObjectId();
37
+ // if (!data.object[0]._id)
38
+ // data.object[0]._id = Crud.ObjectId();
45
39
 
46
- let user = await Crud.send(data)
47
- Elements.setTypeValue(formEl, user)
40
+ // let user = await Crud.send(data)
41
+ // Elements.setTypeValue(formEl, user)
48
42
 
49
43
  // const socket = Crud.socket.getSockets()
50
44
  // if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine) {
51
45
  let key = {
46
+ status: 'await',
52
47
  method: 'create.object',
53
48
  array: 'keys',
54
49
  object: {
55
50
  type: "user",
56
51
  key: user.object[0]._id,
57
52
  roles: ['user'],
58
- email: user.object.email,
59
- password: user.object.password || btoa('0000'),
60
- array
61
- },
62
- organization_id
53
+ email: user.object[0].email,
54
+ password: user.object[0].password || btoa('0000'),
55
+ array: data.array
56
+ }
63
57
  }
64
58
 
65
59
  let response = await Crud.send(key)
66
60
  if (response && response.object && response.object[0]) {
67
- Crud.socket.send({ method: 'signUp', user, userKey })
61
+ // Crud.socket.send({ method: 'signUp', user, userKey })
68
62
 
69
- Render.data({
63
+ render({
70
64
  selector: "[template='signUp']",
71
65
  data: {
72
66
  type: 'signUp',
@@ -82,30 +76,26 @@ const CoCreateUser = {
82
76
  }
83
77
  },
84
78
 
85
- signIn: function (btn) {
86
- let form = btn.closest('form');
87
- let array = form.getAttribute('array');
79
+ signIn: async function (action) {
80
+ if (!action.form) return;
88
81
  let query = [];
89
82
 
90
- const inputs = form.querySelectorAll('input[key="email"], input[key="password"], input[key="username"]');
91
-
92
- inputs.forEach((input) => {
93
- const key = input.getAttribute('key');
94
- const value = input.getValue();
95
- array = 'keys';
83
+ const inputs = action.form.querySelectorAll('input[key="email"], input[key="password"], input[key="username"]');
84
+ for (let i = 0; i < inputs.length; i++) {
85
+ const key = inputs[i].getAttribute('key');
86
+ const value = await inputs[i].getValue();
96
87
  query.push({ key, value, operator: '$eq' })
97
- });
88
+ }
98
89
 
99
90
  let request = {
100
91
  method: 'read.object',
101
- db: 'indexeddb',
102
- array,
92
+ array: 'keys',
103
93
  $filter: {
104
94
  query
105
95
  }
106
96
  }
107
97
 
108
- const socket = Crud.socket.getSockets()
98
+ const socket = await Crud.socket.getSockets()
109
99
  if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine || Crud.socket.serverOrganization == false) {
110
100
  Crud.send(request).then((response) => {
111
101
  response['success'] = false
@@ -130,14 +120,13 @@ const CoCreateUser = {
130
120
  },
131
121
 
132
122
  signInResponse: function (data) {
133
- let { success, status, message, user_id, token } = data;
123
+ let { success, status, message, organization_id, user_id, token } = data;
134
124
  if (success) {
135
125
  localStorage.setItem('organization_id', organization_id);
136
126
  localStorage.setItem("key", Crud.socket.key);
137
127
  localStorage.setItem("host", Crud.socket.host);
138
128
  localStorage.setItem('user_id', user_id);
139
129
  localStorage.setItem("token", token);
140
- // document.cookie = `token=${token};path=/`;
141
130
  message = "Succesful signIn";
142
131
  document.dispatchEvent(new CustomEvent('signIn', {
143
132
  detail: {}
@@ -146,7 +135,7 @@ const CoCreateUser = {
146
135
  else
147
136
  message = "The email or password you entered is incorrect";
148
137
 
149
- Render.data({
138
+ render({
150
139
  selector: "[template='signIn']",
151
140
  data: {
152
141
  type: 'signIn',
@@ -157,18 +146,12 @@ const CoCreateUser = {
157
146
  });
158
147
  },
159
148
 
160
- signOut: (btn) => {
149
+ signOut: () => {
161
150
  self = this;
162
151
  localStorage.removeItem("user_id");
163
152
  localStorage.removeItem("token");
164
153
 
165
- // let allCookies = document.cookie.split(';');
166
-
167
- // for (var i = 0; i < allCookies.length; i++)
168
- // document.cookie = allCookies[i] + "=;expires=" +
169
- // new Date(0).toUTCString();
170
-
171
- Render.data({
154
+ render({
172
155
  selector: "[template='signOut']",
173
156
  data: {
174
157
  type: 'signOut',
@@ -215,8 +198,6 @@ const CoCreateUser = {
215
198
  if (redirectLink) {
216
199
  localStorage.removeItem("user_id");
217
200
  localStorage.removeItem("token");
218
-
219
- // this.deleteCookie();
220
201
  document.location.href = redirectLink;
221
202
  }
222
203
  }
@@ -229,8 +210,7 @@ const CoCreateUser = {
229
210
 
230
211
  if (redirectTag) {
231
212
  Crud.socket.send({
232
- method: 'sendMessage',
233
- message: 'checkSession',
213
+ method: 'checkSession',
234
214
  broadcast: false,
235
215
  broadcastSender: false,
236
216
  broadcastBrowser: false
@@ -245,30 +225,29 @@ const CoCreateUser = {
245
225
  }
246
226
  };
247
227
 
248
-
249
- Action.init({
250
- name: "signUp",
251
- endEvent: "signUp",
252
- callback: (data) => {
253
- CoCreateUser.signUp(data.element);
254
- },
255
- });
256
-
257
- Action.init({
258
- name: "signIn",
259
- endEvent: "signIn",
260
- callback: (data) => {
261
- CoCreateUser.signIn(data.element);
228
+ Actions.init([
229
+ {
230
+ name: "signUp",
231
+ endEvent: "signUp",
232
+ callback: (action) => {
233
+ CoCreateUser.signUp(action);
234
+ }
262
235
  },
263
- });
264
-
265
- Action.init({
266
- name: "signOut",
267
- endEvent: "signOut",
268
- callback: (data) => {
269
- CoCreateUser.signOut(data.element);
236
+ {
237
+ name: "signIn",
238
+ endEvent: "signIn",
239
+ callback: (action) => {
240
+ CoCreateUser.signIn(action);
241
+ }
270
242
  },
271
- });
243
+ {
244
+ name: "signOut",
245
+ endEvent: "signOut",
246
+ callback: (action) => {
247
+ CoCreateUser.signOut(action);
248
+ }
249
+ }
250
+ ]);
272
251
 
273
252
  CoCreateUser.init();
274
253
 
package/src/server.js CHANGED
@@ -14,9 +14,7 @@ class CoCreateUser {
14
14
  }
15
15
 
16
16
  async signUp(data) {
17
- const self = this;
18
17
  try {
19
-
20
18
  if (data.user) {
21
19
  data.user.method = 'create.object'
22
20
  const response = await this.crud.send(data.user)
@@ -29,10 +27,10 @@ class CoCreateUser {
29
27
  this.wsManager.send(response);
30
28
  }
31
29
 
32
- self.wsManager.send(data);
30
+ this.wsManager.send(data);
33
31
 
34
32
  } catch (error) {
35
- console.log('create.object error', error);
33
+ console.log('signup error', error);
36
34
  }
37
35
  }
38
36
 
@@ -51,42 +49,37 @@ class CoCreateUser {
51
49
  const self = this;
52
50
  try {
53
51
  data.method = 'read.object'
52
+ let socket = data.socket
53
+ delete data.socket
54
+
54
55
  this.crud.send(data).then(async (data) => {
55
56
  let response = {
57
+ socket,
56
58
  method: 'signIn',
57
59
  success: false,
58
60
  message: "signIn failed",
59
- status: "failed",
60
61
  userStatus: 'off',
62
+ organization_id: data.organization_id,
61
63
  uid: data.uid
62
64
  }
63
65
 
64
66
  if (data.object[0] && data.object[0]._id && self.wsManager.authenticate) {
65
67
  const user_id = data.object[0].key
66
- const token = await self.wsManager.authenticate.generateToken({ user_id });
68
+ const token = await self.wsManager.authenticate.encodeToken({ user_id });
67
69
 
68
70
  if (token && token != 'null') {
69
- response = {
70
- success: true,
71
- message: "signIn successful",
72
- status: "success",
73
- userStatus: 'on',
74
- user_id,
75
- token,
76
- uid: data.uid
77
- };
78
-
79
- // if (data.organization_id != process.env.organization_id) {
80
- // let Data = { organization_id: process.env.organization_id }
81
- // Data.object['_id'] = data.object[0]._id
82
- // Data.object['lastsignIn'] = data.object[0].lastsignIn
83
- // Data.object['organization_id'] = process.env.organization_id
84
- // crud.send(Data)
85
- // }
71
+ socket.user_id = user_id
72
+ response.success = true
73
+ response.message = "signIn successful"
74
+ response.userStatus = 'on'
75
+ response.user_id = user_id
76
+ response.token = token
77
+ response.userStatus = 'on'
86
78
  }
87
79
  }
88
80
  self.wsManager.send(response)
89
81
  self.wsManager.send({
82
+ socket,
90
83
  method: 'updateUserStatus',
91
84
  user_id: response.user_id,
92
85
  userStatus: response.userStatus,
@@ -106,24 +99,34 @@ class CoCreateUser {
106
99
  async userStatus(data) {
107
100
  const self = this;
108
101
  try {
109
- if (!data.user_id || !data.userStatus)
110
- return
111
- data.array = 'users'
112
- data['object'] = {
113
- _id: data.user_id,
114
- userStatus: data.userStatus
115
- }
102
+ if (data.user_id && data.userStatus) {
103
+ data.array = 'users'
104
+ data['object'] = {
105
+ _id: data.user_id,
106
+ userStatus: data.userStatus
107
+ }
116
108
 
117
- data.method = 'update.object'
118
- this.crud.send(data).then((data) => {
119
- self.wsManager.send({
109
+ data.method = 'update.object'
110
+ data = await this.crud.send(data)
111
+
112
+ if (data.socket)
113
+ self.wsManager.send({
114
+ socket: data.socket,
115
+ method: 'updateUserStatus',
116
+ user_id: data.user_id,
117
+ userStatus: data.userStatus,
118
+ token: data.token,
119
+ organization_id: data.organization_id || socket.organization_id
120
+ })
121
+ } else if (data.socket)
122
+ data.socket.send(JSON.stringify({
120
123
  method: 'updateUserStatus',
121
124
  user_id: data.user_id,
122
125
  userStatus: data.userStatus,
126
+ token: data.token,
123
127
  organization_id: data.organization_id || socket.organization_id
124
- })
128
+ }))
125
129
 
126
- })
127
130
 
128
131
  } catch (error) {
129
132
  console.log('userStatus error')