@cocreate/users 1.27.5 → 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,15 @@
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
+
1
13
  ## [1.27.5](https://github.com/CoCreate-app/CoCreate-users/compare/v1.27.4...v1.27.5) (2023-10-17)
2
14
 
3
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/users",
3
- "version": "1.27.5",
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,29 +76,26 @@ const CoCreateUser = {
82
76
  }
83
77
  },
84
78
 
85
- signIn: async 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"]');
83
+ const inputs = action.form.querySelectorAll('input[key="email"], input[key="password"], input[key="username"]');
91
84
  for (let i = 0; i < inputs.length; i++) {
92
85
  const key = inputs[i].getAttribute('key');
93
86
  const value = await inputs[i].getValue();
94
- array = 'keys';
95
87
  query.push({ key, value, operator: '$eq' })
96
88
  }
97
89
 
98
90
  let request = {
99
91
  method: 'read.object',
100
- db: 'indexeddb',
101
- array,
92
+ array: 'keys',
102
93
  $filter: {
103
94
  query
104
95
  }
105
96
  }
106
97
 
107
- const socket = Crud.socket.getSockets()
98
+ const socket = await Crud.socket.getSockets()
108
99
  if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine || Crud.socket.serverOrganization == false) {
109
100
  Crud.send(request).then((response) => {
110
101
  response['success'] = false
@@ -129,14 +120,13 @@ const CoCreateUser = {
129
120
  },
130
121
 
131
122
  signInResponse: function (data) {
132
- let { success, status, message, user_id, token } = data;
123
+ let { success, status, message, organization_id, user_id, token } = data;
133
124
  if (success) {
134
125
  localStorage.setItem('organization_id', organization_id);
135
126
  localStorage.setItem("key", Crud.socket.key);
136
127
  localStorage.setItem("host", Crud.socket.host);
137
128
  localStorage.setItem('user_id', user_id);
138
129
  localStorage.setItem("token", token);
139
- // document.cookie = `token=${token};path=/`;
140
130
  message = "Succesful signIn";
141
131
  document.dispatchEvent(new CustomEvent('signIn', {
142
132
  detail: {}
@@ -145,7 +135,7 @@ const CoCreateUser = {
145
135
  else
146
136
  message = "The email or password you entered is incorrect";
147
137
 
148
- Render.data({
138
+ render({
149
139
  selector: "[template='signIn']",
150
140
  data: {
151
141
  type: 'signIn',
@@ -156,18 +146,12 @@ const CoCreateUser = {
156
146
  });
157
147
  },
158
148
 
159
- signOut: (btn) => {
149
+ signOut: () => {
160
150
  self = this;
161
151
  localStorage.removeItem("user_id");
162
152
  localStorage.removeItem("token");
163
153
 
164
- // let allCookies = document.cookie.split(';');
165
-
166
- // for (var i = 0; i < allCookies.length; i++)
167
- // document.cookie = allCookies[i] + "=;expires=" +
168
- // new Date(0).toUTCString();
169
-
170
- Render.data({
154
+ render({
171
155
  selector: "[template='signOut']",
172
156
  data: {
173
157
  type: 'signOut',
@@ -214,8 +198,6 @@ const CoCreateUser = {
214
198
  if (redirectLink) {
215
199
  localStorage.removeItem("user_id");
216
200
  localStorage.removeItem("token");
217
-
218
- // this.deleteCookie();
219
201
  document.location.href = redirectLink;
220
202
  }
221
203
  }
@@ -228,8 +210,7 @@ const CoCreateUser = {
228
210
 
229
211
  if (redirectTag) {
230
212
  Crud.socket.send({
231
- method: 'sendMessage',
232
- message: 'checkSession',
213
+ method: 'checkSession',
233
214
  broadcast: false,
234
215
  broadcastSender: false,
235
216
  broadcastBrowser: false
@@ -244,30 +225,29 @@ const CoCreateUser = {
244
225
  }
245
226
  };
246
227
 
247
-
248
- Action.init({
249
- name: "signUp",
250
- endEvent: "signUp",
251
- callback: (data) => {
252
- CoCreateUser.signUp(data.element);
253
- },
254
- });
255
-
256
- Action.init({
257
- name: "signIn",
258
- endEvent: "signIn",
259
- callback: (data) => {
260
- CoCreateUser.signIn(data.element);
228
+ Actions.init([
229
+ {
230
+ name: "signUp",
231
+ endEvent: "signUp",
232
+ callback: (action) => {
233
+ CoCreateUser.signUp(action);
234
+ }
261
235
  },
262
- });
263
-
264
- Action.init({
265
- name: "signOut",
266
- endEvent: "signOut",
267
- callback: (data) => {
268
- CoCreateUser.signOut(data.element);
236
+ {
237
+ name: "signIn",
238
+ endEvent: "signIn",
239
+ callback: (action) => {
240
+ CoCreateUser.signIn(action);
241
+ }
269
242
  },
270
- });
243
+ {
244
+ name: "signOut",
245
+ endEvent: "signOut",
246
+ callback: (action) => {
247
+ CoCreateUser.signOut(action);
248
+ }
249
+ }
250
+ ]);
271
251
 
272
252
  CoCreateUser.init();
273
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')