@live-change/message-authentication-service 0.2.5 → 0.2.10

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.
Files changed (2) hide show
  1. package/index.js +138 -25
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -41,11 +41,18 @@ const targetProperties = {
41
41
  }
42
42
  }
43
43
 
44
+ const messageProperties = {
45
+ messageData: {
46
+ type: Object
47
+ }
48
+ }
49
+
44
50
  const Authentication = definition.model({
45
51
  name: 'Authentication',
46
52
  properties: {
47
53
  ...contactProperties,
48
54
  ...targetProperties,
55
+ ...messageProperties,
49
56
  state: {
50
57
  type: "String",
51
58
  validation: ['nonEmpty'],
@@ -69,11 +76,12 @@ definition.view({
69
76
 
70
77
  definition.event({
71
78
  name: 'authenticationCreated',
72
- execute({ authentication, contactType, contact, action, actionProperties, targetPage }) {
79
+ execute({ authentication, contactType, contact, action, actionProperties, targetPage, messageData }) {
73
80
  return Authentication.create({
74
81
  id: authentication,
75
82
  contactType, contact,
76
83
  action, actionProperties, targetPage,
84
+ messageData,
77
85
  state: 'created'
78
86
  })
79
87
  }
@@ -91,9 +99,11 @@ definition.trigger({
91
99
  waitForEvents: true,
92
100
  properties: {
93
101
  ...contactProperties,
94
- ...targetProperties
102
+ ...targetProperties,
103
+ ...messageProperties
95
104
  },
96
- async execute({ contactType, contact, action, actionProperties, targetPage }, { client, service }, emit) {
105
+ async execute({ contactType, contact, action, actionProperties, targetPage, messageData },
106
+ { client, service }, emit) {
97
107
  const authentication = app.generateUid()
98
108
  const secrets = await service.trigger({
99
109
  type: 'authenticationSecret',
@@ -107,7 +117,8 @@ definition.trigger({
107
117
  action,
108
118
  contactType,
109
119
  contact,
110
- secrets
120
+ secrets,
121
+ ...messageData
111
122
  }
112
123
  })
113
124
  emit({
@@ -117,9 +128,11 @@ definition.trigger({
117
128
  contact,
118
129
  action,
119
130
  actionProperties,
120
- targetPage
131
+ targetPage,
132
+ messageData
121
133
  })
122
134
  return {
135
+ type: 'sent',
123
136
  authentication,
124
137
  secrets: secrets.map(({ secret, ...notSecret }) => notSecret)
125
138
  }
@@ -196,7 +209,8 @@ definition.action({
196
209
  action,
197
210
  contactType,
198
211
  contact,
199
- secrets
212
+ secrets,
213
+ ...authentication.messageData
200
214
  }
201
215
  })
202
216
  return {
@@ -234,74 +248,149 @@ definition.trigger({
234
248
  properties: {
235
249
  ...contactProperties
236
250
  },
237
- async execute({ contactType, contact }, { client, service }, emit) {
251
+ async execute({ contactType, contact, session }, { client, service }, emit) {
238
252
  const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
239
253
  const user = await service.trigger({
240
254
  type: 'signIn' + contactTypeUpperCase,
241
255
  [contactType]: contact,
256
+ session
257
+ })
258
+ return user
259
+ }
260
+ })
261
+
262
+ definition.trigger({
263
+ name: 'connectWithMessageAuthenticated',
264
+ waitForEvents: true,
265
+ properties: {
266
+ ...contactProperties
267
+ },
268
+ async execute({ contactType, contact, user }, { client, service }, emit) {
269
+ const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
270
+ await service.trigger({
271
+ type: 'connect' + contactTypeUpperCase,
272
+ [contactType]: contact,
273
+ user
242
274
  })
243
275
  return user
244
276
  }
245
277
  })
246
278
 
247
279
  for(const contactType of config.contactTypes) {
248
- const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
249
280
 
250
281
  const contactConfig = (typeof contactType == "string") ? { name: contactType } : contactType
251
-
252
282
  const contactTypeName = contactConfig.name
253
283
  const contactTypeUName = contactTypeName[0].toUpperCase() + contactTypeName.slice(1)
254
284
 
255
285
  const contactTypeProperties = {
256
- [contactType]: {
286
+ [contactTypeName]: {
257
287
  type: String,
258
288
  validation: ['nonEmpty', contactTypeName]
259
289
  }
260
290
  }
261
291
 
262
- if(contactConfig.signUp || config.signUp || contactConfig.signIn || config.signIn) {
292
+ if(contactConfig.signUp || config.signUp) {
263
293
  definition.action({
264
- name: 'signIn' + contactTypeUpperCaseName,
294
+ name: 'signUp' + contactTypeUName,
265
295
  waitForEvents: true,
266
296
  properties: {
267
297
  ...contactTypeProperties
268
298
  },
269
299
  async execute({ [contactTypeName]: contact }, { client, service }, emit) {
270
-
300
+ await service.trigger({
301
+ type: 'checkNew' + contactTypeUName,
302
+ [contactTypeName]: contact,
303
+ })
304
+ return service.triggerService(definition.name, {
305
+ type: 'authenticateWithMessage',
306
+ contactType,
307
+ contact,
308
+ action: 'signUpWithMessage',
309
+ targetPage: config.signUpTargetPage || { name: 'user:signUpFinished' }
310
+ })
271
311
  }
272
312
  })
273
313
  }
274
314
 
275
- if(contactConfig.connect || config.connect ) {
315
+ if(contactConfig.signUp || config.signUp || contactConfig.signIn || config.signIn) {
276
316
  definition.action({
277
- name: 'connect',
317
+ name: 'signIn' + contactTypeUName,
318
+ waitForEvents: true,
278
319
  properties: {
279
320
  ...contactTypeProperties
280
321
  },
281
322
  async execute({ [contactTypeName]: contact }, { client, service }, emit) {
282
-
323
+ const contactData = await service.trigger({
324
+ type: 'get' + contactTypeUName,
325
+ [contactTypeName]: contact,
326
+ })
327
+ const messageData = {
328
+ user: contactData.user
329
+ }
330
+ return service.triggerService(definition.name, {
331
+ type: 'authenticateWithMessage',
332
+ contactType,
333
+ contact,
334
+ messageData,
335
+ action: 'signInWithMessage',
336
+ targetPage: config.signInTargetPage || { name: 'user:signInFinished' }
337
+ })
283
338
  }
284
339
  })
285
340
  }
286
341
 
287
- if(contactConfig.signUp || config.signUp) {
342
+ if(contactConfig.connect || config.connect ) {
288
343
  definition.action({
289
- name: 'signUp' + contactTypeUpperCaseName,
290
- waitForEvents: true,
344
+ name: 'connect' + contactTypeUName,
291
345
  properties: {
292
346
  ...contactTypeProperties
293
347
  },
348
+ access: (params, { client }) => {
349
+ return !!client.user
350
+ },
294
351
  async execute({ [contactTypeName]: contact }, { client, service }, emit) {
295
352
  await service.trigger({
296
353
  type: 'checkNew' + contactTypeUName,
297
- [contactType]: contact,
354
+ [contactTypeName]: contact,
298
355
  })
299
356
  return service.triggerService(definition.name, {
300
357
  type: 'authenticateWithMessage',
301
358
  contactType,
302
359
  contact,
303
- action: 'signUpWithMessage',
304
- targetPage: config.signUpTargetPage || { name: 'user:signUpFinished' }
360
+ action: 'connectWithMessage',
361
+ actionProperties: {
362
+ user: client.user
363
+ },
364
+ messageData: {
365
+ user: client.user
366
+ },
367
+ targetPage: config.signUpTargetPage || { name: 'user:connectFinished' }
368
+ })
369
+ }
370
+ })
371
+
372
+ definition.action({
373
+ name: 'disconnect' + contactTypeUName,
374
+ properties: {
375
+ ...contactTypeProperties
376
+ },
377
+ access: (params, { client }) => {
378
+ return !!client.user
379
+ },
380
+ async execute({ [contactTypeName]: contact }, { client, service }, emit) {
381
+ const contacts = (await service.trigger({
382
+ type: 'getConnectedContacts',
383
+ user: client.user
384
+ })).flat()
385
+ console.log("CONTACTS", contacts, contactTypeName, contact)
386
+ const contactData = contacts.find(c => c.type == contactTypeName && c.contact == contact)
387
+ if(!contactData) throw 'notFound'
388
+ if(contacts.length == 1) throw 'lastOne'
389
+ console.log("DISCONNECT", contact)
390
+ return await service.trigger({
391
+ type: 'disconnect' + contactTypeUName,
392
+ [contactTypeName]: contact,
393
+ user: client.user
305
394
  })
306
395
  }
307
396
  })
@@ -309,13 +398,37 @@ for(const contactType of config.contactTypes) {
309
398
 
310
399
  if(contactConfig.signUp || config.signUp) {
311
400
  definition.action({
312
- name: 'signInOrSignUp' + contactTypeUpperCaseName,
401
+ name: 'signInOrSignUp' + contactTypeUName,
313
402
  waitForEvents: true,
314
403
  properties: {
315
404
  ...contactTypeProperties
316
405
  },
317
- async execute({ [contactType]: contact }, { client, service }, emit) {
318
-
406
+ async execute({ [contactTypeName]: contact }, { client, service }, emit) {
407
+ const contactData = await service.trigger({
408
+ type: 'get' + contactTypeUName + 'OrNull',
409
+ [contactTypeName]: contact,
410
+ })
411
+ if(contactData) {
412
+ const messageData = {
413
+ user: contactData.user
414
+ }
415
+ return service.triggerService(definition.name, {
416
+ type: 'authenticateWithMessage',
417
+ contactType,
418
+ contact,
419
+ messageData,
420
+ action: 'signInWithMessage',
421
+ targetPage: config.signInTargetPage || { name: 'user:signInFinished' }
422
+ })
423
+ } else {
424
+ return service.triggerService(definition.name, {
425
+ type: 'authenticateWithMessage',
426
+ contactType,
427
+ contact,
428
+ action: 'signUpWithMessage',
429
+ targetPage: config.signUpTargetPage || { name: 'user:signUpFinished' }
430
+ })
431
+ }
319
432
  }
320
433
  })
321
434
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/message-authentication-service",
3
- "version": "0.2.5",
3
+ "version": "0.2.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -24,5 +24,5 @@
24
24
  "@live-change/framework": "^0.5.7",
25
25
  "nodemailer": "^6.7.2"
26
26
  },
27
- "gitHead": "e9f8af40d836388f6847cf958c1d941dc8e273b1"
27
+ "gitHead": "ae86524e5ac7052eb6b6e7b34e319373b6bdef23"
28
28
  }