@flowerforce/flowerbase 1.7.4-beta.0 → 1.7.4-beta.3

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/controller.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAQzC;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,GAAG,EAAE,eAAe,iBA2KxD"}
1
+ {"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/auth/controller.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AASzC;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,GAAG,EAAE,eAAe,iBAsLxD"}
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.authController = authController;
13
13
  const bson_1 = require("bson");
14
14
  const constants_1 = require("../constants");
15
+ const state_1 = require("../state");
15
16
  const crypto_1 = require("../utils/crypto");
16
17
  const utils_1 = require("./utils");
17
18
  const HANDLER_TYPE = 'preHandler';
@@ -53,20 +54,29 @@ function authController(app) {
53
54
  }
54
55
  }, function (req) {
55
56
  return __awaiter(this, void 0, void 0, function* () {
57
+ var _a, _b;
56
58
  if (req.user.typ !== 'access') {
57
59
  throw new Error('Access token required');
58
60
  }
59
- const user = yield db
61
+ const authUser = yield db
60
62
  .collection(authCollection)
61
63
  .findOne({ _id: bson_1.ObjectId.createFromHexString(req.user.id) });
64
+ const customData = userCollection && constants_1.AUTH_CONFIG.user_id_field
65
+ ? yield db
66
+ .collection(userCollection)
67
+ .findOne({ [constants_1.AUTH_CONFIG.user_id_field]: req.user.id })
68
+ : null;
69
+ const params = req.params;
70
+ const stateProjectId = state_1.StateManager.select('projectId');
71
+ const domainId = (_b = (_a = params === null || params === void 0 ? void 0 : params.appId) !== null && _a !== void 0 ? _a : stateProjectId) !== null && _b !== void 0 ? _b : '';
62
72
  return {
63
- _id: user === null || user === void 0 ? void 0 : user._id.toString(),
64
- identities: user === null || user === void 0 ? void 0 : user.identities,
73
+ user_id: req.user.id,
74
+ domain_id: domainId,
75
+ identities: authUser === null || authUser === void 0 ? void 0 : authUser.identities,
76
+ custom_data: customData !== null && customData !== void 0 ? customData : {},
65
77
  type: 'normal',
66
- custom_data: user === null || user === void 0 ? void 0 : user.curstom_data,
67
78
  data: {
68
- _id: user === null || user === void 0 ? void 0 : user._id.toString(),
69
- email: user === null || user === void 0 ? void 0 : user.email
79
+ email: authUser === null || authUser === void 0 ? void 0 : authUser.email
70
80
  }
71
81
  };
72
82
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowerforce/flowerbase",
3
- "version": "1.7.4-beta.0",
3
+ "version": "1.7.4-beta.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -48,4 +48,68 @@ describe('authController', () => {
48
48
  { unique: true }
49
49
  )
50
50
  })
51
+
52
+ it('returns Realm-like profile payload with linked custom_data', async () => {
53
+ const authUserId = '697349de5dc2c5850198cc06'
54
+ let profileHandler: ((req: any) => Promise<unknown>) | undefined
55
+ const authCollection = {
56
+ createIndex: jest.fn().mockResolvedValue('ok'),
57
+ findOne: jest.fn().mockResolvedValue({
58
+ _id: { toString: () => authUserId },
59
+ email: 'jessica.lussu@stackhouse.it',
60
+ identities: [{ provider_type: 'local-userpass' }]
61
+ })
62
+ }
63
+ const usersCollection = {
64
+ findOne: jest.fn().mockResolvedValue({
65
+ _id: '6075cb8840ceb66546e9aaeb',
66
+ userId: authUserId,
67
+ name: 'Jessica Lussu'
68
+ })
69
+ }
70
+ const refreshCollection = {
71
+ createIndex: jest.fn().mockResolvedValue('ok')
72
+ }
73
+ const db = {
74
+ collection: jest.fn((name: string) => {
75
+ if (name === 'auth_users') return authCollection
76
+ if (name === 'users') return usersCollection
77
+ if (name === 'refresh_tokens') return refreshCollection
78
+ return { createIndex: jest.fn().mockResolvedValue('ok') }
79
+ })
80
+ }
81
+ const app = {
82
+ mongo: { client: { db: jest.fn().mockReturnValue(db) } },
83
+ addHook: jest.fn(),
84
+ get: jest.fn((path: string, _opts: unknown, handler: (req: any) => Promise<unknown>) => {
85
+ if (path === '/profile') {
86
+ profileHandler = handler
87
+ }
88
+ }),
89
+ post: jest.fn(),
90
+ delete: jest.fn(),
91
+ jwtAuthentication: jest.fn()
92
+ }
93
+
94
+ await authController(app as unknown as never)
95
+ const result = await profileHandler?.({
96
+ user: { typ: 'access', id: authUserId },
97
+ params: { appId: 'flowerbase-e2e' }
98
+ })
99
+
100
+ expect(result).toEqual({
101
+ user_id: authUserId,
102
+ domain_id: 'flowerbase-e2e',
103
+ identities: [{ provider_type: 'local-userpass' }],
104
+ custom_data: {
105
+ _id: '6075cb8840ceb66546e9aaeb',
106
+ userId: authUserId,
107
+ name: 'Jessica Lussu'
108
+ },
109
+ type: 'normal',
110
+ data: {
111
+ email: 'jessica.lussu@stackhouse.it'
112
+ }
113
+ })
114
+ })
51
115
  })
@@ -1,6 +1,7 @@
1
1
  import { ObjectId } from 'bson'
2
2
  import { FastifyInstance } from 'fastify'
3
3
  import { AUTH_CONFIG, DB_NAME, DEFAULT_CONFIG } from '../constants'
4
+ import { StateManager } from '../state'
4
5
  import { hashToken } from '../utils/crypto'
5
6
  import { SessionCreatedDto } from './dtos'
6
7
  import { AUTH_ENDPOINTS, AUTH_ERRORS } from './utils'
@@ -55,17 +56,28 @@ export async function authController(app: FastifyInstance) {
55
56
  if (req.user.typ !== 'access') {
56
57
  throw new Error('Access token required')
57
58
  }
58
- const user = await db
59
+ const authUser = await db
59
60
  .collection<Record<string, unknown>>(authCollection)
60
61
  .findOne({ _id: ObjectId.createFromHexString(req.user.id) })
62
+
63
+ const customData = userCollection && AUTH_CONFIG.user_id_field
64
+ ? await db
65
+ .collection<Record<string, unknown>>(userCollection)
66
+ .findOne({ [AUTH_CONFIG.user_id_field]: req.user.id })
67
+ : null
68
+
69
+ const params = (req as unknown as { params?: { appId?: string } }).params
70
+ const stateProjectId = StateManager.select('projectId')
71
+ const domainId = params?.appId ?? stateProjectId ?? ''
72
+
61
73
  return {
62
- _id: user?._id.toString(),
63
- identities: user?.identities,
74
+ user_id: req.user.id,
75
+ domain_id: domainId,
76
+ identities: authUser?.identities,
77
+ custom_data: customData ?? {},
64
78
  type: 'normal',
65
- custom_data: user?.curstom_data,
66
79
  data: {
67
- _id: user?._id.toString(),
68
- email: user?.email
80
+ email: authUser?.email
69
81
  }
70
82
  }
71
83
  })