adminforth 1.1.102 → 1.1.104

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/dist/index.js CHANGED
@@ -101,7 +101,7 @@ class AdminForth {
101
101
  catch (e) {
102
102
  this.config.customization.customComponentsDir = undefined;
103
103
  }
104
- if (this.config.auth) {
104
+ if (!this.config.auth) {
105
105
  if (!this.config.auth.resourceId) {
106
106
  throw new Error('No config.auth.resourceId defined');
107
107
  }
@@ -118,6 +118,9 @@ class AdminForth {
118
118
  if (!userResource) {
119
119
  throw new Error(`Resource with id "${this.config.auth.resourceId}" not found`);
120
120
  }
121
+ if (!this.config.auth.beforeLoginConfirmation) {
122
+ this.config.auth.beforeLoginConfirmation = [];
123
+ }
121
124
  }
122
125
  if (!this.config.customization) {
123
126
  this.config.customization = {};
@@ -570,7 +573,7 @@ class AdminForth {
570
573
  method: 'POST',
571
574
  path: '/login',
572
575
  handler: (_b) => __awaiter(this, [_b], void 0, function* ({ body, response }) {
573
- var _c, _d, _e, _f, _g, _h;
576
+ var _c, _d, _e, _f;
574
577
  const INVALID_MESSAGE = 'Invalid username or password';
575
578
  const { username, password } = body;
576
579
  let adminUser;
@@ -614,19 +617,14 @@ class AdminForth {
614
617
  adminUser = {
615
618
  isRoot: false, dbUser: userRecord,
616
619
  pk: userRecord[userResource.columns.find((col) => col.primaryKey).name],
617
- username
620
+ username,
618
621
  };
619
622
  const beforeLoginConfirmation = this.config.auth.beforeLoginConfirmation;
620
623
  if (beforeLoginConfirmation === null || beforeLoginConfirmation === void 0 ? void 0 : beforeLoginConfirmation.length) {
621
624
  for (const hook of beforeLoginConfirmation) {
622
- const resp = yield hook({ userRecord: adminUser });
623
- if ((_d = resp === null || resp === void 0 ? void 0 : resp.body) === null || _d === void 0 ? void 0 : _d.setCookie) {
624
- (_e = resp === null || resp === void 0 ? void 0 : resp.body) === null || _e === void 0 ? void 0 : _e.setCookie.forEach((cookie) => {
625
- this.auth.setCustomCookie({ response, payload: cookie });
626
- });
627
- }
628
- if ((_f = resp === null || resp === void 0 ? void 0 : resp.body) === null || _f === void 0 ? void 0 : _f.redirectTo) {
629
- toReturn = { ok: resp.ok, redirectTo: (_g = resp === null || resp === void 0 ? void 0 : resp.body) === null || _g === void 0 ? void 0 : _g.redirectTo, allowedLogin: (_h = resp === null || resp === void 0 ? void 0 : resp.body) === null || _h === void 0 ? void 0 : _h.allowedLogin };
625
+ const resp = yield hook({ adminUser, response });
626
+ if ((_d = resp === null || resp === void 0 ? void 0 : resp.body) === null || _d === void 0 ? void 0 : _d.redirectTo) {
627
+ toReturn = { ok: resp.ok, redirectTo: (_e = resp === null || resp === void 0 ? void 0 : resp.body) === null || _e === void 0 ? void 0 : _e.redirectTo, allowedLogin: (_f = resp === null || resp === void 0 ? void 0 : resp.body) === null || _f === void 0 ? void 0 : _f.allowedLogin };
630
628
  break;
631
629
  }
632
630
  }
@@ -645,7 +643,7 @@ class AdminForth {
645
643
  server.endpoint({
646
644
  method: 'POST',
647
645
  path: '/check_auth',
648
- handler: (_j) => __awaiter(this, [_j], void 0, function* ({ adminUser }) {
646
+ handler: (_g) => __awaiter(this, [_g], void 0, function* ({ adminUser }) {
649
647
  return { ok: true };
650
648
  }),
651
649
  });
@@ -653,7 +651,7 @@ class AdminForth {
653
651
  noAuth: true,
654
652
  method: 'POST',
655
653
  path: '/logout',
656
- handler: (_k) => __awaiter(this, [_k], void 0, function* ({ response }) {
654
+ handler: (_h) => __awaiter(this, [_h], void 0, function* ({ response }) {
657
655
  this.auth.removeAuthCookie(response);
658
656
  return { ok: true };
659
657
  }),
@@ -662,8 +660,8 @@ class AdminForth {
662
660
  noAuth: true,
663
661
  method: 'GET',
664
662
  path: '/get_public_config',
665
- handler: (_l) => __awaiter(this, [_l], void 0, function* ({ body }) {
666
- var _m;
663
+ handler: (_j) => __awaiter(this, [_j], void 0, function* ({ body }) {
664
+ var _k;
667
665
  // find resource
668
666
  if (!this.config.auth) {
669
667
  throw new Error('No config.auth defined');
@@ -675,15 +673,15 @@ class AdminForth {
675
673
  brandName: this.config.customization.brandName,
676
674
  usernameFieldName: usernameColumn.label,
677
675
  loginBackgroundImage: this.config.auth.loginBackgroundImage,
678
- title: (_m = this.config.customization) === null || _m === void 0 ? void 0 : _m.title,
676
+ title: (_k = this.config.customization) === null || _k === void 0 ? void 0 : _k.title,
679
677
  };
680
678
  }),
681
679
  });
682
680
  server.endpoint({
683
681
  method: 'GET',
684
682
  path: '/get_base_config',
685
- handler: (_o) => __awaiter(this, [_o], void 0, function* ({ input, adminUser, cookies }) {
686
- var _p, _q;
683
+ handler: (_l) => __awaiter(this, [_l], void 0, function* ({ input, adminUser, cookies }) {
684
+ var _m, _o;
687
685
  let username = '';
688
686
  let userFullName = '';
689
687
  if (adminUser.isRoot) {
@@ -744,8 +742,8 @@ class AdminForth {
744
742
  deleteConfirmation: this.config.deleteConfirmation,
745
743
  auth: this.config.auth,
746
744
  usernameField: this.config.auth.usernameField,
747
- title: (_p = this.config.customization) === null || _p === void 0 ? void 0 : _p.title,
748
- emptyFieldPlaceholder: (_q = this.config.customization) === null || _q === void 0 ? void 0 : _q.emptyFieldPlaceholder,
745
+ title: (_m = this.config.customization) === null || _m === void 0 ? void 0 : _m.title,
746
+ emptyFieldPlaceholder: (_o = this.config.customization) === null || _o === void 0 ? void 0 : _o.emptyFieldPlaceholder,
749
747
  },
750
748
  adminUser,
751
749
  version: ADMINFORTH_VERSION,
@@ -784,7 +782,7 @@ class AdminForth {
784
782
  server.endpoint({
785
783
  method: 'POST',
786
784
  path: '/get_resource',
787
- handler: (_r) => __awaiter(this, [_r], void 0, function* ({ body, adminUser }) {
785
+ handler: (_p) => __awaiter(this, [_p], void 0, function* ({ body, adminUser }) {
788
786
  const { resourceId } = body;
789
787
  if (!this.statuses.dbDiscover) {
790
788
  return { error: 'Database discovery not started' };
@@ -806,8 +804,8 @@ class AdminForth {
806
804
  server.endpoint({
807
805
  method: 'POST',
808
806
  path: '/get_resource_data',
809
- handler: (_s) => __awaiter(this, [_s], void 0, function* ({ body, adminUser }) {
810
- var _t, _u, _v, _w;
807
+ handler: (_q) => __awaiter(this, [_q], void 0, function* ({ body, adminUser }) {
808
+ var _r, _s, _t, _u;
811
809
  const { resourceId, source } = body;
812
810
  if (['show', 'list'].includes(source) === false) {
813
811
  return { error: 'Invalid source, should be list or show' };
@@ -827,7 +825,7 @@ class AdminForth {
827
825
  if (!allowed) {
828
826
  return { error };
829
827
  }
830
- for (const hook of listify((_u = (_t = resource.hooks) === null || _t === void 0 ? void 0 : _t[source]) === null || _u === void 0 ? void 0 : _u.beforeDatasourceRequest)) {
828
+ for (const hook of listify((_s = (_r = resource.hooks) === null || _r === void 0 ? void 0 : _r[source]) === null || _s === void 0 ? void 0 : _s.beforeDatasourceRequest)) {
831
829
  const resp = yield hook({ resource, query: body, adminUser });
832
830
  if (!resp || (!resp.ok && !resp.error)) {
833
831
  throw new Error(`Hook must return object with {ok: true} or { error: 'Error' } `);
@@ -894,7 +892,7 @@ class AdminForth {
894
892
  item[col.name] = targetDataMap[item[col.name]];
895
893
  });
896
894
  })));
897
- for (const hook of listify((_w = (_v = resource.hooks) === null || _v === void 0 ? void 0 : _v[source]) === null || _w === void 0 ? void 0 : _w.afterDatasourceResponse)) {
895
+ for (const hook of listify((_u = (_t = resource.hooks) === null || _t === void 0 ? void 0 : _t[source]) === null || _u === void 0 ? void 0 : _u.afterDatasourceResponse)) {
898
896
  const resp = yield hook({ resource, response: data.data, adminUser });
899
897
  if (!resp || (!resp.ok && !resp.error)) {
900
898
  throw new Error(`Hook must return object with {ok: true} or { error: 'Error' } `);
@@ -920,8 +918,8 @@ class AdminForth {
920
918
  server.endpoint({
921
919
  method: 'POST',
922
920
  path: '/get_resource_foreign_data',
923
- handler: (_x) => __awaiter(this, [_x], void 0, function* ({ body, adminUser }) {
924
- var _y, _z, _0, _1;
921
+ handler: (_v) => __awaiter(this, [_v], void 0, function* ({ body, adminUser }) {
922
+ var _w, _x, _y, _z;
925
923
  const { resourceId, column } = body;
926
924
  if (!this.statuses.dbDiscover) {
927
925
  return { error: 'Database discovery not started' };
@@ -942,7 +940,7 @@ class AdminForth {
942
940
  }
943
941
  const targetResourceId = columnConfig.foreignResource.resourceId;
944
942
  const targetResource = this.config.resources.find((res) => res.resourceId == targetResourceId);
945
- for (const hook of listify((_z = (_y = columnConfig.foreignResource.hooks) === null || _y === void 0 ? void 0 : _y.dropdownList) === null || _z === void 0 ? void 0 : _z.beforeDatasourceRequest)) {
943
+ for (const hook of listify((_x = (_w = columnConfig.foreignResource.hooks) === null || _w === void 0 ? void 0 : _w.dropdownList) === null || _x === void 0 ? void 0 : _x.beforeDatasourceRequest)) {
946
944
  const resp = yield hook({ query: body, adminUser, resource: targetResource });
947
945
  if (!resp || (!resp.ok && !resp.error)) {
948
946
  throw new Error(`Hook must return object with {ok: true} or { error: 'Error' } `);
@@ -971,7 +969,7 @@ class AdminForth {
971
969
  const response = {
972
970
  items
973
971
  };
974
- for (const hook of listify((_1 = (_0 = columnConfig.foreignResource.hooks) === null || _0 === void 0 ? void 0 : _0.dropdownList) === null || _1 === void 0 ? void 0 : _1.afterDatasourceResponse)) {
972
+ for (const hook of listify((_z = (_y = columnConfig.foreignResource.hooks) === null || _y === void 0 ? void 0 : _y.dropdownList) === null || _z === void 0 ? void 0 : _z.afterDatasourceResponse)) {
975
973
  const resp = yield hook({ response, adminUser, resource: targetResource });
976
974
  if (!resp || (!resp.ok && !resp.error)) {
977
975
  throw new Error(`Hook must return object with {ok: true} or { error: 'Error' } `);
@@ -986,7 +984,7 @@ class AdminForth {
986
984
  server.endpoint({
987
985
  method: 'POST',
988
986
  path: '/get_min_max_for_columns',
989
- handler: (_2) => __awaiter(this, [_2], void 0, function* ({ body }) {
987
+ handler: (_0) => __awaiter(this, [_0], void 0, function* ({ body }) {
990
988
  const { resourceId } = body;
991
989
  if (!this.statuses.dbDiscover) {
992
990
  return { error: 'Database discovery not started' };
@@ -1015,7 +1013,7 @@ class AdminForth {
1015
1013
  server.endpoint({
1016
1014
  method: 'POST',
1017
1015
  path: '/create_record',
1018
- handler: (_3) => __awaiter(this, [_3], void 0, function* ({ body, adminUser }) {
1016
+ handler: (_1) => __awaiter(this, [_1], void 0, function* ({ body, adminUser }) {
1019
1017
  const resource = this.config.resources.find((res) => res.resourceId == body['resourceId']);
1020
1018
  if (!resource) {
1021
1019
  return { error: `Resource '${body['resourceId']}' not found` };
@@ -1036,8 +1034,8 @@ class AdminForth {
1036
1034
  server.endpoint({
1037
1035
  method: 'POST',
1038
1036
  path: '/update_record',
1039
- handler: (_4) => __awaiter(this, [_4], void 0, function* ({ body, adminUser }) {
1040
- var _5, _6, _7, _8;
1037
+ handler: (_2) => __awaiter(this, [_2], void 0, function* ({ body, adminUser }) {
1038
+ var _3, _4, _5, _6;
1041
1039
  const resource = this.config.resources.find((res) => res.resourceId == body['resourceId']);
1042
1040
  if (!resource) {
1043
1041
  return { error: `Resource '${body['resourceId']}' not found` };
@@ -1056,7 +1054,7 @@ class AdminForth {
1056
1054
  return { error };
1057
1055
  }
1058
1056
  // execute hook if needed
1059
- for (const hook of listify((_6 = (_5 = resource.hooks) === null || _5 === void 0 ? void 0 : _5.edit) === null || _6 === void 0 ? void 0 : _6.beforeSave)) {
1057
+ for (const hook of listify((_4 = (_3 = resource.hooks) === null || _3 === void 0 ? void 0 : _3.edit) === null || _4 === void 0 ? void 0 : _4.beforeSave)) {
1060
1058
  const resp = yield hook({ resource, record, adminUser });
1061
1059
  if (!resp || (!resp.ok && !resp.error)) {
1062
1060
  throw new Error(`Hook beforeSave must return object with {ok: true} or { error: 'Error' } `);
@@ -1083,7 +1081,7 @@ class AdminForth {
1083
1081
  yield connector.updateRecord({ resource, recordId, newValues });
1084
1082
  }
1085
1083
  // execute hook if needed
1086
- for (const hook of listify((_8 = (_7 = resource.hooks) === null || _7 === void 0 ? void 0 : _7.edit) === null || _8 === void 0 ? void 0 : _8.afterSave)) {
1084
+ for (const hook of listify((_6 = (_5 = resource.hooks) === null || _5 === void 0 ? void 0 : _5.edit) === null || _6 === void 0 ? void 0 : _6.afterSave)) {
1087
1085
  const resp = yield hook({ resource, record, adminUser, oldRecord });
1088
1086
  if (!resp || (!resp.ok && !resp.error)) {
1089
1087
  throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
@@ -1100,8 +1098,8 @@ class AdminForth {
1100
1098
  server.endpoint({
1101
1099
  method: 'POST',
1102
1100
  path: '/delete_record',
1103
- handler: (_9) => __awaiter(this, [_9], void 0, function* ({ body, adminUser }) {
1104
- var _10, _11, _12, _13;
1101
+ handler: (_7) => __awaiter(this, [_7], void 0, function* ({ body, adminUser }) {
1102
+ var _8, _9, _10, _11;
1105
1103
  const resource = this.config.resources.find((res) => res.resourceId == body['resourceId']);
1106
1104
  const record = yield this.connectors[resource.dataSource].getRecordByPrimaryKey(resource, body['primaryKey']);
1107
1105
  if (!resource) {
@@ -1119,7 +1117,7 @@ class AdminForth {
1119
1117
  return { error };
1120
1118
  }
1121
1119
  // execute hook if needed
1122
- for (const hook of listify((_11 = (_10 = resource.hooks) === null || _10 === void 0 ? void 0 : _10.delete) === null || _11 === void 0 ? void 0 : _11.beforeSave)) {
1120
+ for (const hook of listify((_9 = (_8 = resource.hooks) === null || _8 === void 0 ? void 0 : _8.delete) === null || _9 === void 0 ? void 0 : _9.beforeSave)) {
1123
1121
  const resp = yield hook({ resource, record, adminUser });
1124
1122
  if (!resp || (!resp.ok && !resp.error)) {
1125
1123
  throw new Error(`Hook beforeSave must return object with {ok: true} or { error: 'Error' } `);
@@ -1131,7 +1129,7 @@ class AdminForth {
1131
1129
  const connector = this.connectors[resource.dataSource];
1132
1130
  yield connector.deleteRecord({ resource, recordId: body['primaryKey'] });
1133
1131
  // execute hook if needed
1134
- for (const hook of listify((_13 = (_12 = resource.hooks) === null || _12 === void 0 ? void 0 : _12.delete) === null || _13 === void 0 ? void 0 : _13.afterSave)) {
1132
+ for (const hook of listify((_11 = (_10 = resource.hooks) === null || _10 === void 0 ? void 0 : _10.delete) === null || _11 === void 0 ? void 0 : _11.afterSave)) {
1135
1133
  const resp = yield hook({ resource, record, adminUser });
1136
1134
  if (!resp || (!resp.ok && !resp.error)) {
1137
1135
  throw new Error(`Hook afterSave must return object with {ok: true} or { error: 'Error' } `);
@@ -1148,7 +1146,7 @@ class AdminForth {
1148
1146
  server.endpoint({
1149
1147
  method: 'POST',
1150
1148
  path: '/start_bulk_action',
1151
- handler: (_14) => __awaiter(this, [_14], void 0, function* ({ body }) {
1149
+ handler: (_12) => __awaiter(this, [_12], void 0, function* ({ body }) {
1152
1150
  const { resourceId, actionId, recordIds } = body;
1153
1151
  const resource = this.config.resources.find((res) => res.resourceId == resourceId);
1154
1152
  if (!resource) {
@@ -1,3 +1,4 @@
1
+ ;
1
2
  export var AdminForthMenuTypes;
2
3
  (function (AdminForthMenuTypes) {
3
4
  /**
package/index.ts CHANGED
@@ -138,7 +138,7 @@ class AdminForth implements IAdminForth {
138
138
  this.config.customization.customComponentsDir = undefined;
139
139
  }
140
140
 
141
- if (this.config.auth) {
141
+ if (!this.config.auth) {
142
142
  if (!this.config.auth.resourceId) {
143
143
  throw new Error('No config.auth.resourceId defined');
144
144
  }
@@ -155,6 +155,10 @@ class AdminForth implements IAdminForth {
155
155
  if (!userResource) {
156
156
  throw new Error(`Resource with id "${this.config.auth.resourceId}" not found`);
157
157
  }
158
+
159
+ if (!this.config.auth.beforeLoginConfirmation) {
160
+ this.config.auth.beforeLoginConfirmation = [];
161
+ }
158
162
  }
159
163
 
160
164
  if (!this.config.customization) {
@@ -708,16 +712,13 @@ class AdminForth implements IAdminForth {
708
712
  adminUser = {
709
713
  isRoot: false, dbUser: userRecord,
710
714
  pk: userRecord[userResource.columns.find((col) => col.primaryKey).name],
711
- username
715
+ username,
712
716
  };
713
717
  const beforeLoginConfirmation = this.config.auth.beforeLoginConfirmation as (BeforeLoginConfirmationFunction[] | undefined);
714
718
  if (beforeLoginConfirmation?.length){
715
719
  for (const hook of beforeLoginConfirmation) {
716
- const resp = await hook({ userRecord:adminUser});
717
- if (resp?.body?.setCookie){
718
- resp?.body?.setCookie.forEach((cookie) => {
719
- this.auth.setCustomCookie({response,payload:cookie})});
720
- }
720
+ const resp = await hook({ adminUser, response });
721
+
721
722
  if (resp?.body?.redirectTo) {
722
723
  toReturn = {ok:resp.ok, redirectTo:resp?.body?.redirectTo, allowedLogin:resp?.body?.allowedLogin};
723
724
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adminforth",
3
- "version": "1.1.102",
3
+ "version": "1.1.104",
4
4
  "description": "OpenSource Vue3 powered forth-generation admin panel",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -5,6 +5,11 @@ export interface ICodeInjector {
5
5
  allComponentNames: Object;
6
6
  }
7
7
 
8
+ export interface IAdminForthHttpResponse {
9
+ setHeader: (key: string, value: string) => void,
10
+ setStatus: (code: number, message: string) => void,
11
+ };
12
+
8
13
  /**
9
14
  * Implement this interface to create custom HTTP server adapter for AdminForth.
10
15
  */
@@ -28,10 +33,13 @@ export interface IHttpServer {
28
33
  method: string,
29
34
  noAuth?: boolean,
30
35
  path: string,
31
- handler: (body: any, adminUser: any, query: {[key: string]: string}, headers: {[key: string]: string}, cookies: {[key: string]: string}, response: {
32
- setHeader: (key: string, value: string) => void,
33
- setStatus: (code: number, message: string) => void,
34
- }) => void,
36
+ handler: (
37
+ body: any,
38
+ adminUser: any,
39
+ query: {[key: string]: string},
40
+ headers: {[key: string]: string}, cookies: {[key: string]: string},
41
+ response: IAdminForthHttpResponse,
42
+ ) => void,
35
43
  }): void;
36
44
 
37
45
  }
@@ -615,7 +623,17 @@ export type AfterSaveFunction = (params: {resource: AdminForthResource, adminUse
615
623
  /**
616
624
  * Allow to get user data before login confirmation, will triger when user try to login.
617
625
  */
618
- export type BeforeLoginConfirmationFunction = (params?: { userRecord: AdminUser }) => Promise<{ok:boolean, error?:string, body:{setCookie?:[], redirectTo?: 'string', allowedLogin?: boolean }}>;
626
+ export type BeforeLoginConfirmationFunction = (params?: {
627
+ adminUser: AdminUser,
628
+ response: IAdminForthHttpResponse,
629
+ }) => Promise<{
630
+ ok:boolean,
631
+ error?:string,
632
+ body: {
633
+ redirectTo?: 'string',
634
+ allowedLogin?: boolean,
635
+ }
636
+ }>;
619
637
 
620
638
  /**
621
639
  * Resource describes one table or collection in database.