@aeriajs/builtins 0.0.113 → 0.0.115

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,6 +1,6 @@
1
1
  import type { Context } from '@aeriajs/types';
2
2
  import type { description } from './description.js';
3
- import { HTTPStatus } from '@aeriajs/types';
3
+ import { Result, ACError, HTTPStatus } from '@aeriajs/types';
4
4
  export declare enum ActivationError {
5
5
  UserNotFound = "USER_NOT_FOUND",
6
6
  AlreadyActiveUser = "ALREADY_ACTIVE_USER",
@@ -8,16 +8,36 @@ export declare enum ActivationError {
8
8
  }
9
9
  export declare const activate: (payload: {
10
10
  password: string;
11
- }, context: Context<typeof description>) => Promise<import("@aeriajs/types").GenericResponse | import("@aeriajs/types").Result.Error<{
11
+ }, context: Context<typeof description> & {
12
+ request: {
13
+ query: {
14
+ u?: string;
15
+ t?: string;
16
+ };
17
+ payload: {
18
+ password?: string;
19
+ };
20
+ };
21
+ }) => Promise<import("@aeriajs/types").GenericResponse | Result.Error<{
12
22
  readonly code: ActivationError.InvalidLink;
13
23
  } & {
14
24
  httpStatus: HTTPStatus.NotFound;
15
- }> | import("@aeriajs/types").Result.Error<{
25
+ }> | Result.Error<{
16
26
  readonly code: ActivationError.UserNotFound;
17
27
  } & {
18
28
  httpStatus: HTTPStatus.NotFound;
19
- }> | import("@aeriajs/types").Result.Error<{
29
+ }> | Result.Error<{
20
30
  readonly code: ActivationError.AlreadyActiveUser;
21
31
  } & {
22
32
  httpStatus: HTTPStatus.Forbidden;
23
- }> | undefined>;
33
+ }> | Result.Error<{
34
+ readonly code: ACError.MalformedInput;
35
+ } & {
36
+ httpStatus: HTTPStatus.UnprocessableContent;
37
+ }> | {
38
+ readonly _tag: "Result";
39
+ readonly error: undefined;
40
+ readonly result: {
41
+ readonly userId: any;
42
+ };
43
+ } | undefined>;
@@ -4,6 +4,7 @@ exports.activate = exports.ActivationError = void 0;
4
4
  const core_1 = require("@aeriajs/core");
5
5
  const types_1 = require("@aeriajs/types");
6
6
  const bcrypt = require("bcrypt");
7
+ const getActivationLink_js_1 = require("./getActivationLink.js");
7
8
  var ActivationError;
8
9
  (function (ActivationError) {
9
10
  ActivationError["UserNotFound"] = "USER_NOT_FOUND";
@@ -34,7 +35,8 @@ const activate = async (payload, context) => {
34
35
  code: ActivationError.AlreadyActiveUser,
35
36
  });
36
37
  }
37
- const equal = await bcrypt.compare(user._id.toString(), token);
38
+ const activationToken = await (0, getActivationLink_js_1.getActivationToken)(userId.toString(), context);
39
+ const equal = await bcrypt.compare(activationToken, token);
38
40
  if (!equal) {
39
41
  return context.error(types_1.HTTPStatus.NotFound, {
40
42
  code: ActivationError.InvalidLink,
@@ -42,8 +44,13 @@ const activate = async (payload, context) => {
42
44
  }
43
45
  if (!user.password) {
44
46
  if (!payload.password) {
45
- return context.response.writeHead(302, {
46
- location: `/user/activation?step=password&u=${userId}&t=${token}`,
47
+ if (context.request.method === 'GET') {
48
+ return context.response.writeHead(302, {
49
+ location: `/user/activation?step=password&u=${userId}&t=${token}`,
50
+ }).end();
51
+ }
52
+ return context.error(types_1.HTTPStatus.UnprocessableContent, {
53
+ code: types_1.ACError.MalformedInput,
47
54
  });
48
55
  }
49
56
  await context.collection.model.updateOne({
@@ -63,8 +70,13 @@ const activate = async (payload, context) => {
63
70
  active: true,
64
71
  },
65
72
  });
66
- return context.response.writeHead(302, {
67
- location: '/user/activation',
73
+ if (context.request.method === 'GET') {
74
+ return context.response.writeHead(302, {
75
+ location: '/user/activation',
76
+ }).end();
77
+ }
78
+ return types_1.Result.result({
79
+ userId: user._id,
68
80
  });
69
81
  };
70
82
  exports.activate = activate;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  import { ObjectId } from "@aeriajs/core";
3
- import { HTTPStatus } from "@aeriajs/types";
3
+ import { Result, ACError, HTTPStatus } from "@aeriajs/types";
4
4
  import * as bcrypt from "bcrypt";
5
+ import { getActivationToken } from "./getActivationLink.mjs";
5
6
  export var ActivationError = /* @__PURE__ */ ((ActivationError2) => {
6
7
  ActivationError2["UserNotFound"] = "USER_NOT_FOUND";
7
8
  ActivationError2["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
@@ -35,7 +36,8 @@ export const activate = async (payload, context) => {
35
36
  code: "ALREADY_ACTIVE_USER" /* AlreadyActiveUser */
36
37
  });
37
38
  }
38
- const equal = await bcrypt.compare(user._id.toString(), token);
39
+ const activationToken = await getActivationToken(userId.toString(), context);
40
+ const equal = await bcrypt.compare(activationToken, token);
39
41
  if (!equal) {
40
42
  return context.error(HTTPStatus.NotFound, {
41
43
  code: "INVALID_LINK" /* InvalidLink */
@@ -43,8 +45,13 @@ export const activate = async (payload, context) => {
43
45
  }
44
46
  if (!user.password) {
45
47
  if (!payload.password) {
46
- return context.response.writeHead(302, {
47
- location: `/user/activation?step=password&u=${userId}&t=${token}`
48
+ if (context.request.method === "GET") {
49
+ return context.response.writeHead(302, {
50
+ location: `/user/activation?step=password&u=${userId}&t=${token}`
51
+ }).end();
52
+ }
53
+ return context.error(HTTPStatus.UnprocessableContent, {
54
+ code: ACError.MalformedInput
48
55
  });
49
56
  }
50
57
  await context.collection.model.updateOne(
@@ -70,7 +77,12 @@ export const activate = async (payload, context) => {
70
77
  }
71
78
  }
72
79
  );
73
- return context.response.writeHead(302, {
74
- location: "/user/activation"
80
+ if (context.request.method === "GET") {
81
+ return context.response.writeHead(302, {
82
+ location: "/user/activation"
83
+ }).end();
84
+ }
85
+ return Result.result({
86
+ userId: user._id
75
87
  });
76
88
  };
@@ -7,9 +7,6 @@ export declare const description: {
7
7
  readonly required: readonly ["name", "roles", "email"];
8
8
  readonly form: readonly ["name", "active", "roles", "email", "phone_number", "picture_file"];
9
9
  readonly indexes: readonly ["name"];
10
- readonly freshItem: {
11
- readonly active: true;
12
- };
13
10
  readonly properties: {
14
11
  readonly name: {
15
12
  readonly type: "string";
@@ -86,6 +83,11 @@ export declare const description: {
86
83
  readonly fetchItem: true;
87
84
  readonly translate: true;
88
85
  };
86
+ readonly copyActivationLink: {
87
+ readonly label: "copy_activation_link";
88
+ readonly icon: "link";
89
+ readonly translate: true;
90
+ };
89
91
  };
90
92
  readonly icon: "users";
91
93
  readonly filters: readonly ["name", "roles", "email", "phone_number"];
@@ -22,9 +22,6 @@ exports.description = (0, core_1.defineDescription)({
22
22
  'picture_file',
23
23
  ],
24
24
  indexes: ['name'],
25
- freshItem: {
26
- active: true,
27
- },
28
25
  properties: {
29
26
  name: {
30
27
  type: 'string',
@@ -111,6 +108,11 @@ exports.description = (0, core_1.defineDescription)({
111
108
  fetchItem: true,
112
109
  translate: true,
113
110
  },
111
+ 'copyActivationLink': {
112
+ label: 'copy_activation_link',
113
+ icon: 'link',
114
+ translate: true,
115
+ },
114
116
  },
115
117
  icon: 'users',
116
118
  filters: [
@@ -16,9 +16,6 @@ export const description = defineDescription({
16
16
  "picture_file"
17
17
  ],
18
18
  indexes: ["name"],
19
- freshItem: {
20
- active: true
21
- },
22
19
  properties: {
23
20
  name: {
24
21
  type: "string"
@@ -104,6 +101,11 @@ export const description = defineDescription({
104
101
  icon: "key",
105
102
  fetchItem: true,
106
103
  translate: true
104
+ },
105
+ "copyActivationLink": {
106
+ label: "copy_activation_link",
107
+ icon: "link",
108
+ translate: true
107
109
  }
108
110
  },
109
111
  icon: "users",
@@ -1,6 +1,22 @@
1
+ import type { Context } from '@aeriajs/types';
1
2
  import type { ObjectId } from '@aeriajs/core';
2
- export declare const getActivationLink: (userId: ObjectId) => Promise<{
3
+ import { Result, HTTPStatus } from '@aeriajs/types';
4
+ import { ActivationError } from './activate.js';
5
+ export declare const getActivationToken: (strId: string, context: Context) => Promise<string>;
6
+ export declare const getActivationLink: (payload: {
7
+ userId: ObjectId | string;
8
+ }, context: Context) => Promise<{
9
+ readonly _tag: "Error";
10
+ readonly error: import("@aeriajs/types").StrictEndpointError<import("@aeriajs/types").ACError.ResourceNotFound | import("@aeriajs/types").ACError.MalformedInput, unknown, HTTPStatus.BadRequest | HTTPStatus.NotFound>;
11
+ readonly result: undefined;
12
+ } | Result.Error<{
13
+ readonly code: ActivationError.AlreadyActiveUser;
14
+ } & {
15
+ httpStatus: HTTPStatus.Forbidden;
16
+ }> | {
3
17
  readonly _tag: "Result";
4
18
  readonly error: undefined;
5
- readonly result: string;
19
+ readonly result: {
20
+ readonly url: string;
21
+ };
6
22
  }>;
@@ -1,13 +1,39 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getActivationLink = void 0;
4
- const entrypoint_1 = require("@aeriajs/entrypoint");
3
+ exports.getActivationLink = exports.getActivationToken = void 0;
5
4
  const types_1 = require("@aeriajs/types");
6
5
  const bcrypt = require("bcrypt");
7
- const getActivationLink = async (userId) => {
8
- const config = await (0, entrypoint_1.getConfig)();
9
- const activationToken = await bcrypt.hash(userId.toString(), 10);
10
- const link = `${config.publicUrl}/user/activate?u=${userId.toString()}&t=${activationToken}`;
11
- return types_1.Result.result(link);
6
+ const activate_js_1 = require("./activate.js");
7
+ const getActivationToken = async (strId, context) => {
8
+ if (context.calledFunction === 'getActivationToken') {
9
+ throw new Error('cannot be called externally');
10
+ }
11
+ if (!context.config.secret) {
12
+ throw new Error('config.secret is not set');
13
+ }
14
+ return `${context.config.secret}:${strId}`;
15
+ };
16
+ exports.getActivationToken = getActivationToken;
17
+ const getActivationLink = async (payload, context) => {
18
+ const { error, result: user } = await context.collections.user.functions.get({
19
+ filters: {
20
+ _id: payload.userId,
21
+ },
22
+ project: ['active'],
23
+ });
24
+ if (error) {
25
+ return types_1.Result.error(error);
26
+ }
27
+ if (user.active) {
28
+ return context.error(types_1.HTTPStatus.Forbidden, {
29
+ code: activate_js_1.ActivationError.AlreadyActiveUser,
30
+ });
31
+ }
32
+ const activationToken = await (0, exports.getActivationToken)(payload.userId.toString(), context);
33
+ const encryptedActivationToken = await bcrypt.hash(activationToken, 10);
34
+ const url = `${context.config.publicUrl}/user/activate?u=${payload.userId.toString()}&t=${encryptedActivationToken}`;
35
+ return types_1.Result.result({
36
+ url,
37
+ });
12
38
  };
13
39
  exports.getActivationLink = getActivationLink;
@@ -1,10 +1,35 @@
1
1
  "use strict";
2
- import { getConfig } from "@aeriajs/entrypoint";
3
- import { Result } from "@aeriajs/types";
2
+ import { Result, HTTPStatus } from "@aeriajs/types";
4
3
  import * as bcrypt from "bcrypt";
5
- export const getActivationLink = async (userId) => {
6
- const config = await getConfig();
7
- const activationToken = await bcrypt.hash(userId.toString(), 10);
8
- const link = `${config.publicUrl}/user/activate?u=${userId.toString()}&t=${activationToken}`;
9
- return Result.result(link);
4
+ import { ActivationError } from "./activate.mjs";
5
+ export const getActivationToken = async (strId, context) => {
6
+ if (context.calledFunction === "getActivationToken") {
7
+ throw new Error("cannot be called externally");
8
+ }
9
+ if (!context.config.secret) {
10
+ throw new Error("config.secret is not set");
11
+ }
12
+ return `${context.config.secret}:${strId}`;
13
+ };
14
+ export const getActivationLink = async (payload, context) => {
15
+ const { error, result: user } = await context.collections.user.functions.get({
16
+ filters: {
17
+ _id: payload.userId
18
+ },
19
+ project: ["active"]
20
+ });
21
+ if (error) {
22
+ return Result.error(error);
23
+ }
24
+ if (user.active) {
25
+ return context.error(HTTPStatus.Forbidden, {
26
+ code: ActivationError.AlreadyActiveUser
27
+ });
28
+ }
29
+ const activationToken = await getActivationToken(payload.userId.toString(), context);
30
+ const encryptedActivationToken = await bcrypt.hash(activationToken, 10);
31
+ const url = `${context.config.publicUrl}/user/activate?u=${payload.userId.toString()}&t=${encryptedActivationToken}`;
32
+ return Result.result({
33
+ url
34
+ });
10
35
  };
@@ -13,9 +13,6 @@ export declare const getCurrentUser: (_payload: undefined, context: Context<type
13
13
  readonly required: readonly ["name", "roles", "email"];
14
14
  readonly form: readonly ["name", "active", "roles", "email", "phone_number", "picture_file"];
15
15
  readonly indexes: readonly ["name"];
16
- readonly freshItem: {
17
- readonly active: true;
18
- };
19
16
  readonly properties: {
20
17
  readonly name: {
21
18
  readonly type: "string";
@@ -92,6 +89,11 @@ export declare const getCurrentUser: (_payload: undefined, context: Context<type
92
89
  readonly fetchItem: true;
93
90
  readonly translate: true;
94
91
  };
92
+ readonly copyActivationLink: {
93
+ readonly label: "copy_activation_link";
94
+ readonly icon: "link";
95
+ readonly translate: true;
96
+ };
95
97
  };
96
98
  readonly icon: "users";
97
99
  readonly filters: readonly ["name", "roles", "email", "phone_number"];
@@ -4,6 +4,7 @@ exports.getInfo = exports.ActivationError = void 0;
4
4
  const bcrypt = require("bcrypt");
5
5
  const types_1 = require("@aeriajs/types");
6
6
  const core_1 = require("@aeriajs/core");
7
+ const getActivationLink_js_1 = require("./getActivationLink.js");
7
8
  var ActivationError;
8
9
  (function (ActivationError) {
9
10
  ActivationError["UserNotFound"] = "USER_NOT_FOUND";
@@ -30,7 +31,8 @@ const getInfo = async (payload, context) => {
30
31
  code: ActivationError.AlreadyActiveUser,
31
32
  });
32
33
  }
33
- const equal = await bcrypt.compare(user._id.toString(), token);
34
+ const activationToken = await (0, getActivationLink_js_1.getActivationToken)(user._id.toString(), context);
35
+ const equal = await bcrypt.compare(activationToken, token);
34
36
  if (!equal) {
35
37
  return context.error(types_1.HTTPStatus.NotFound, {
36
38
  code: ActivationError.InvalidLink,
@@ -2,6 +2,7 @@
2
2
  import * as bcrypt from "bcrypt";
3
3
  import { Result, HTTPStatus } from "@aeriajs/types";
4
4
  import { ObjectId } from "@aeriajs/core";
5
+ import { getActivationToken } from "./getActivationLink.mjs";
5
6
  export var ActivationError = /* @__PURE__ */ ((ActivationError2) => {
6
7
  ActivationError2["UserNotFound"] = "USER_NOT_FOUND";
7
8
  ActivationError2["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
@@ -31,7 +32,8 @@ export const getInfo = async (payload, context) => {
31
32
  code: "ALREADY_ACTIVE_USER" /* AlreadyActiveUser */
32
33
  });
33
34
  }
34
- const equal = await bcrypt.compare(user._id.toString(), token);
35
+ const activationToken = await getActivationToken(user._id.toString(), context);
36
+ const equal = await bcrypt.compare(activationToken, token);
35
37
  if (!equal) {
36
38
  return context.error(HTTPStatus.NotFound, {
37
39
  code: "INVALID_LINK" /* InvalidLink */