@flowerforce/flowerbase 1.7.5-beta.0 → 1.7.5-beta.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.
@@ -1 +1 @@
1
- {"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../../src/auth/providers/custom-function/controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AASzC;;;;GAIG;AACH,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,eAAe,iBAiHlE"}
1
+ {"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../../../src/auth/providers/custom-function/controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAUzC;;;;GAIG;AACH,wBAAsB,wBAAwB,CAAC,GAAG,EAAE,eAAe,iBA2IlE"}
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.customFunctionController = customFunctionController;
13
+ const mongodb_1 = require("mongodb");
13
14
  const constants_1 = require("../../../constants");
14
15
  const state_1 = require("../../../state");
15
16
  const context_1 = require("../../../utils/context");
@@ -36,7 +37,10 @@ function customFunctionController(app) {
36
37
  * @returns {Promise<Object>} A promise resolving with access and refresh tokens.
37
38
  */
38
39
  app.post(utils_1.AUTH_ENDPOINTS.LOGIN, {
39
- schema: schema_1.LOGIN_SCHEMA
40
+ schema: schema_1.LOGIN_SCHEMA,
41
+ errorHandler: (_error, _request, reply) => {
42
+ reply.code(500).send({ message: 'Internal Server Error' });
43
+ }
40
44
  }, function (req, reply) {
41
45
  return __awaiter(this, void 0, void 0, function* () {
42
46
  var _a, _b;
@@ -75,10 +79,29 @@ function customFunctionController(app) {
75
79
  reply.code(401).send({ message: 'Unauthorized' });
76
80
  return;
77
81
  }
78
- const authUser = yield db.collection(authCollection).findOne({ email: authResult.id });
82
+ const email = (authResult === null || authResult === void 0 ? void 0 : authResult.email) || authResult.id;
83
+ let authUser = yield db.collection(authCollection).findOne({ email });
79
84
  if (!authUser) {
80
- reply.code(401).send({ message: 'Unauthorized' });
81
- return;
85
+ const authUserId = new mongodb_1.ObjectId();
86
+ yield db.collection(authCollection).insertOne({
87
+ _id: authUserId,
88
+ email,
89
+ status: 'confirmed',
90
+ createdAt: new Date(),
91
+ custom_data: {},
92
+ identities: [
93
+ {
94
+ id: authResult.id.toString(),
95
+ provider_id: authResult.id.toString(),
96
+ provider_type: 'custom-function',
97
+ provider_data: { email }
98
+ }
99
+ ]
100
+ });
101
+ authUser = {
102
+ _id: authUserId,
103
+ email
104
+ };
82
105
  }
83
106
  const user = user_id_field && userCollection
84
107
  ? yield db
@@ -99,12 +122,14 @@ function customFunctionController(app) {
99
122
  expiresAt: new Date(Date.now() + refreshTokenTtlMs),
100
123
  revokedAt: null
101
124
  });
102
- return {
103
- access_token: this.createAccessToken(currentUserData),
125
+ const accessToken = this.createAccessToken(currentUserData);
126
+ const responsePayload = {
127
+ access_token: accessToken,
104
128
  refresh_token: refreshToken,
105
129
  device_id: '',
106
130
  user_id: authUser._id.toString()
107
131
  };
132
+ reply.code(200).send(responsePayload);
108
133
  });
109
134
  });
110
135
  });
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/utils.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,aAAa,EAAW,QAAQ,EAAE,MAAM,aAAa,CAAA;AA0E9D;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAU,gBAAuB,KAAG,OAAO,CAAC,QAAQ,CAkB5E,CAAA;AA+mBD,eAAO,MAAM,gBAAgB;kHAxlB1B,aAAa;iHAggBb,aAAa;uHA/Yb,aAAa;CA2ef,CAAA"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/utils.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,aAAa,EAAW,QAAQ,EAAE,MAAM,aAAa,CAAA;AA0E9D;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAU,gBAAuB,KAAG,OAAO,CAAC,QAAQ,CAkB5E,CAAA;AA0nBD,eAAO,MAAM,gBAAgB;kHAnmB1B,aAAa;iHA2gBb,aAAa;uHA1Zb,aAAa;CAsff,CAAA"}
@@ -252,6 +252,14 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
252
252
  if (shouldIgnoreStreamError(error))
253
253
  return;
254
254
  console.error('Authentication trigger change stream error', error);
255
+ emitTriggerEvent({
256
+ status: 'error',
257
+ triggerName,
258
+ triggerType,
259
+ functionName,
260
+ meta: Object.assign(Object.assign({}, baseMeta), { event: 'CHANGE_STREAM' }),
261
+ error
262
+ });
255
263
  });
256
264
  changeStream.on('change', function (change) {
257
265
  return __awaiter(this, void 0, void 0, function* () {
@@ -321,13 +329,6 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
321
329
  updateDescription
322
330
  };
323
331
  try {
324
- emitTriggerEvent({
325
- status: 'fired',
326
- triggerName,
327
- triggerType,
328
- functionName,
329
- meta: Object.assign(Object.assign({}, baseMeta), { event: 'LOGOUT' })
330
- });
331
332
  yield (0, context_1.GenerateContext)({
332
333
  args: [Object.assign({ user: userData }, op)],
333
334
  app,
@@ -339,6 +340,13 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
339
340
  services,
340
341
  runAsSystem: true
341
342
  });
343
+ emitTriggerEvent({
344
+ status: 'fired',
345
+ triggerName,
346
+ triggerType,
347
+ functionName,
348
+ meta: Object.assign(Object.assign({}, baseMeta), { event: 'LOGOUT' })
349
+ });
342
350
  }
343
351
  catch (error) {
344
352
  emitTriggerEvent({
@@ -373,13 +381,6 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
373
381
  updateDescription
374
382
  };
375
383
  try {
376
- emitTriggerEvent({
377
- status: 'fired',
378
- triggerName,
379
- triggerType,
380
- functionName,
381
- meta: Object.assign(Object.assign({}, baseMeta), { event: 'DELETE' })
382
- });
383
384
  yield (0, context_1.GenerateContext)({
384
385
  args: isAutoTrigger ? [userData] : [Object.assign({ user: userData }, op)],
385
386
  app,
@@ -391,6 +392,13 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
391
392
  services,
392
393
  runAsSystem: true
393
394
  });
395
+ emitTriggerEvent({
396
+ status: 'fired',
397
+ triggerName,
398
+ triggerType,
399
+ functionName,
400
+ meta: Object.assign(Object.assign({}, baseMeta), { event: 'DELETE' })
401
+ });
394
402
  }
395
403
  catch (error) {
396
404
  emitTriggerEvent({
@@ -427,13 +435,6 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
427
435
  updateDescription
428
436
  };
429
437
  try {
430
- emitTriggerEvent({
431
- status: 'fired',
432
- triggerName,
433
- triggerType,
434
- functionName,
435
- meta: Object.assign(Object.assign({}, baseMeta), { event: 'UPDATE' })
436
- });
437
438
  yield (0, context_1.GenerateContext)({
438
439
  args: isAutoTrigger ? [userData] : [Object.assign({ user: userData }, op)],
439
440
  app,
@@ -445,6 +446,13 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
445
446
  services,
446
447
  runAsSystem: true
447
448
  });
449
+ emitTriggerEvent({
450
+ status: 'fired',
451
+ triggerName,
452
+ triggerType,
453
+ functionName,
454
+ meta: Object.assign(Object.assign({}, baseMeta), { event: 'UPDATE' })
455
+ });
448
456
  }
449
457
  catch (error) {
450
458
  emitTriggerEvent({
@@ -518,13 +526,6 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
518
526
  updateDescription
519
527
  };
520
528
  try {
521
- emitTriggerEvent({
522
- status: 'fired',
523
- triggerName,
524
- triggerType,
525
- functionName,
526
- meta: Object.assign(Object.assign({}, baseMeta), { event: 'CREATE' })
527
- });
528
529
  yield (0, context_1.GenerateContext)({
529
530
  args: isAutoTrigger ? [userData] : [Object.assign({ user: userData }, op)],
530
531
  app,
@@ -536,6 +537,13 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
536
537
  services,
537
538
  runAsSystem: true
538
539
  });
540
+ emitTriggerEvent({
541
+ status: 'fired',
542
+ triggerName,
543
+ triggerType,
544
+ functionName,
545
+ meta: Object.assign(Object.assign({}, baseMeta), { event: 'CREATE' })
546
+ });
539
547
  }
540
548
  catch (error) {
541
549
  emitTriggerEvent({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowerforce/flowerbase",
3
- "version": "1.7.5-beta.0",
3
+ "version": "1.7.5-beta.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,4 +1,5 @@
1
1
  import { FastifyInstance } from 'fastify'
2
+ import { ObjectId } from 'mongodb'
2
3
  import { AUTH_CONFIG, DB_NAME, DEFAULT_CONFIG } from '../../../constants'
3
4
  import { StateManager } from '../../../state'
4
5
  import { GenerateContext } from '../../../utils/context'
@@ -30,7 +31,10 @@ export async function customFunctionController(app: FastifyInstance) {
30
31
  app.post<LoginDto>(
31
32
  AUTH_ENDPOINTS.LOGIN,
32
33
  {
33
- schema: LOGIN_SCHEMA
34
+ schema: LOGIN_SCHEMA,
35
+ errorHandler: (_error, _request, reply) => {
36
+ reply.code(500).send({ message: 'Internal Server Error' })
37
+ }
34
38
  },
35
39
  async function (req, reply) {
36
40
  const customFunctionProvider = AUTH_CONFIG.authProviders?.['custom-function']
@@ -54,7 +58,7 @@ export async function customFunctionController(app: FastifyInstance) {
54
58
  id
55
59
  } = req
56
60
 
57
- type CustomFunctionAuthResult = { id?: string }
61
+ type CustomFunctionAuthResult = { id?: string; email?: string }
58
62
  const authResult = await GenerateContext({
59
63
  args: [
60
64
  req.body
@@ -77,16 +81,35 @@ export async function customFunctionController(app: FastifyInstance) {
77
81
  }
78
82
  }) as CustomFunctionAuthResult
79
83
 
80
-
81
84
  if (!authResult.id) {
82
85
  reply.code(401).send({ message: 'Unauthorized' })
83
86
  return
84
87
  }
85
88
 
86
- const authUser = await db.collection(authCollection!).findOne({ email: authResult.id })
89
+ const email = authResult?.email || authResult.id
90
+ let authUser = await db.collection(authCollection!).findOne({ email })
91
+
87
92
  if (!authUser) {
88
- reply.code(401).send({ message: 'Unauthorized' })
89
- return
93
+ const authUserId = new ObjectId()
94
+ await db.collection(authCollection!).insertOne({
95
+ _id: authUserId,
96
+ email,
97
+ status: 'confirmed',
98
+ createdAt: new Date(),
99
+ custom_data: {},
100
+ identities: [
101
+ {
102
+ id: authResult.id.toString(),
103
+ provider_id: authResult.id.toString(),
104
+ provider_type: 'custom-function',
105
+ provider_data: { email }
106
+ }
107
+ ]
108
+ })
109
+ authUser = {
110
+ _id: authUserId,
111
+ email
112
+ }
90
113
  }
91
114
 
92
115
  const user =
@@ -107,6 +130,7 @@ export async function customFunctionController(app: FastifyInstance) {
107
130
  ...(user || {})
108
131
  }
109
132
  }
133
+
110
134
  const refreshToken = this.createRefreshToken(currentUserData)
111
135
  const refreshTokenHash = hashToken(refreshToken)
112
136
  await db.collection(refreshTokensCollection).insertOne({
@@ -116,12 +140,15 @@ export async function customFunctionController(app: FastifyInstance) {
116
140
  expiresAt: new Date(Date.now() + refreshTokenTtlMs),
117
141
  revokedAt: null
118
142
  })
119
- return {
120
- access_token: this.createAccessToken(currentUserData),
143
+ const accessToken = this.createAccessToken(currentUserData)
144
+
145
+ const responsePayload = {
146
+ access_token: accessToken,
121
147
  refresh_token: refreshToken,
122
148
  device_id: '',
123
149
  user_id: authUser._id.toString()
124
150
  }
151
+ reply.code(200).send(responsePayload)
125
152
  }
126
153
  )
127
154
 
@@ -277,6 +277,17 @@ const handleAuthenticationTrigger = async ({
277
277
  changeStream.on('error', (error) => {
278
278
  if (shouldIgnoreStreamError(error)) return
279
279
  console.error('Authentication trigger change stream error', error)
280
+ emitTriggerEvent({
281
+ status: 'error',
282
+ triggerName,
283
+ triggerType,
284
+ functionName,
285
+ meta: {
286
+ ...baseMeta,
287
+ event: 'CHANGE_STREAM'
288
+ },
289
+ error
290
+ })
280
291
  })
281
292
  changeStream.on('change', async function (change) {
282
293
  const operationType = change['operationType' as keyof typeof change] as
@@ -365,13 +376,6 @@ const handleAuthenticationTrigger = async ({
365
376
  updateDescription
366
377
  }
367
378
  try {
368
- emitTriggerEvent({
369
- status: 'fired',
370
- triggerName,
371
- triggerType,
372
- functionName,
373
- meta: { ...baseMeta, event: 'LOGOUT' }
374
- })
375
379
  await GenerateContext({
376
380
  args: [{ user: userData, ...op }],
377
381
  app,
@@ -383,6 +387,13 @@ const handleAuthenticationTrigger = async ({
383
387
  services,
384
388
  runAsSystem: true
385
389
  })
390
+ emitTriggerEvent({
391
+ status: 'fired',
392
+ triggerName,
393
+ triggerType,
394
+ functionName,
395
+ meta: { ...baseMeta, event: 'LOGOUT' }
396
+ })
386
397
  } catch (error) {
387
398
  emitTriggerEvent({
388
399
  status: 'error',
@@ -417,13 +428,6 @@ const handleAuthenticationTrigger = async ({
417
428
  updateDescription
418
429
  }
419
430
  try {
420
- emitTriggerEvent({
421
- status: 'fired',
422
- triggerName,
423
- triggerType,
424
- functionName,
425
- meta: { ...baseMeta, event: 'DELETE' }
426
- })
427
431
  await GenerateContext({
428
432
  args: isAutoTrigger ? [userData] : [{ user: userData, ...op }],
429
433
  app,
@@ -435,6 +439,13 @@ const handleAuthenticationTrigger = async ({
435
439
  services,
436
440
  runAsSystem: true
437
441
  })
442
+ emitTriggerEvent({
443
+ status: 'fired',
444
+ triggerName,
445
+ triggerType,
446
+ functionName,
447
+ meta: { ...baseMeta, event: 'DELETE' }
448
+ })
438
449
  } catch (error) {
439
450
  emitTriggerEvent({
440
451
  status: 'error',
@@ -471,13 +482,6 @@ const handleAuthenticationTrigger = async ({
471
482
  updateDescription
472
483
  }
473
484
  try {
474
- emitTriggerEvent({
475
- status: 'fired',
476
- triggerName,
477
- triggerType,
478
- functionName,
479
- meta: { ...baseMeta, event: 'UPDATE' }
480
- })
481
485
  await GenerateContext({
482
486
  args: isAutoTrigger ? [userData] : [{ user: userData, ...op }],
483
487
  app,
@@ -489,6 +493,13 @@ const handleAuthenticationTrigger = async ({
489
493
  services,
490
494
  runAsSystem: true
491
495
  })
496
+ emitTriggerEvent({
497
+ status: 'fired',
498
+ triggerName,
499
+ triggerType,
500
+ functionName,
501
+ meta: { ...baseMeta, event: 'UPDATE' }
502
+ })
492
503
  } catch (error) {
493
504
  emitTriggerEvent({
494
505
  status: 'error',
@@ -575,13 +586,6 @@ const handleAuthenticationTrigger = async ({
575
586
  }
576
587
 
577
588
  try {
578
- emitTriggerEvent({
579
- status: 'fired',
580
- triggerName,
581
- triggerType,
582
- functionName,
583
- meta: { ...baseMeta, event: 'CREATE' }
584
- })
585
589
  await GenerateContext({
586
590
  args: isAutoTrigger ? [userData] : [{ user: userData, ...op }],
587
591
  app,
@@ -593,6 +597,13 @@ const handleAuthenticationTrigger = async ({
593
597
  services,
594
598
  runAsSystem: true
595
599
  })
600
+ emitTriggerEvent({
601
+ status: 'fired',
602
+ triggerName,
603
+ triggerType,
604
+ functionName,
605
+ meta: { ...baseMeta, event: 'CREATE' }
606
+ })
596
607
  } catch (error) {
597
608
  emitTriggerEvent({
598
609
  status: 'error',