@aeriajs/builtins 0.0.0
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/LICENSE +19 -0
- package/README.md +6 -0
- package/dist/collections/file/description.d.ts +50 -0
- package/dist/collections/file/description.js +70 -0
- package/dist/collections/file/description.mjs +258 -0
- package/dist/collections/file/download.d.ts +13 -0
- package/dist/collections/file/download.js +78 -0
- package/dist/collections/file/download.mjs +232 -0
- package/dist/collections/file/index.d.ts +1183 -0
- package/dist/collections/file/index.js +49 -0
- package/dist/collections/file/index.mjs +46 -0
- package/dist/collections/file/insert.d.ts +7 -0
- package/dist/collections/file/insert.js +44 -0
- package/dist/collections/file/insert.mjs +246 -0
- package/dist/collections/file/remove.d.ts +3 -0
- package/dist/collections/file/remove.js +28 -0
- package/dist/collections/file/remove.mjs +184 -0
- package/dist/collections/file/removeAll.d.ts +3 -0
- package/dist/collections/file/removeAll.js +30 -0
- package/dist/collections/file/removeAll.mjs +194 -0
- package/dist/collections/index.d.ts +4 -0
- package/dist/collections/index.js +20 -0
- package/dist/collections/index.mjs +4 -0
- package/dist/collections/log/index.d.ts +458 -0
- package/dist/collections/log/index.js +46 -0
- package/dist/collections/log/index.mjs +45 -0
- package/dist/collections/resourceUsage/index.d.ts +57 -0
- package/dist/collections/resourceUsage/index.js +19 -0
- package/dist/collections/resourceUsage/index.mjs +16 -0
- package/dist/collections/user/activate.d.ts +10 -0
- package/dist/collections/user/activate.js +65 -0
- package/dist/collections/user/activate.mjs +247 -0
- package/dist/collections/user/authenticate.d.ts +24 -0
- package/dist/collections/user/authenticate.js +103 -0
- package/dist/collections/user/authenticate.mjs +398 -0
- package/dist/collections/user/createAccount.d.ts +101 -0
- package/dist/collections/user/createAccount.js +66 -0
- package/dist/collections/user/createAccount.mjs +219 -0
- package/dist/collections/user/description.d.ts +103 -0
- package/dist/collections/user/description.js +146 -0
- package/dist/collections/user/description.mjs +151 -0
- package/dist/collections/user/getActivationLink.d.ts +2 -0
- package/dist/collections/user/getActivationLink.js +15 -0
- package/dist/collections/user/getActivationLink.mjs +156 -0
- package/dist/collections/user/getInfo.d.ts +14 -0
- package/dist/collections/user/getInfo.js +39 -0
- package/dist/collections/user/getInfo.mjs +192 -0
- package/dist/collections/user/index.d.ts +3992 -0
- package/dist/collections/user/index.js +37 -0
- package/dist/collections/user/index.mjs +36 -0
- package/dist/collections/user/insert.d.ts +5 -0
- package/dist/collections/user/insert.js +35 -0
- package/dist/collections/user/insert.mjs +156 -0
- package/dist/functions/describe.d.ts +2 -0
- package/dist/functions/describe.js +54 -0
- package/dist/functions/describe.mjs +302 -0
- package/dist/functions/index.d.ts +1 -0
- package/dist/functions/index.js +17 -0
- package/dist/functions/index.mjs +1 -0
- package/dist/index.d.ts +5701 -0
- package/dist/index.js +39 -0
- package/dist/index.mjs +11 -0
- package/package.json +60 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.log = void 0;
|
|
4
|
+
const api_1 = require("@aeriajs/api");
|
|
5
|
+
exports.log = (0, api_1.defineCollection)({
|
|
6
|
+
description: {
|
|
7
|
+
$id: 'log',
|
|
8
|
+
required: [
|
|
9
|
+
'context',
|
|
10
|
+
'message',
|
|
11
|
+
],
|
|
12
|
+
properties: {
|
|
13
|
+
owner: {
|
|
14
|
+
// don't use "owned: true", we want it this way
|
|
15
|
+
$ref: 'user',
|
|
16
|
+
noForm: true,
|
|
17
|
+
},
|
|
18
|
+
context: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
},
|
|
21
|
+
message: {
|
|
22
|
+
type: 'string',
|
|
23
|
+
},
|
|
24
|
+
details: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
variable: true,
|
|
27
|
+
},
|
|
28
|
+
created_at: {
|
|
29
|
+
type: 'string',
|
|
30
|
+
format: 'date-time',
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
icon: 'magnifying-glass',
|
|
34
|
+
presets: ['view'],
|
|
35
|
+
filters: [
|
|
36
|
+
'context',
|
|
37
|
+
'message',
|
|
38
|
+
'owner',
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
functions: {
|
|
42
|
+
get: api_1.get,
|
|
43
|
+
getAll: api_1.getAll,
|
|
44
|
+
insert: api_1.insert,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { defineCollection, get, getAll, insert } from "@aeriajs/api";
|
|
2
|
+
export var log = defineCollection({
|
|
3
|
+
description: {
|
|
4
|
+
$id: "log",
|
|
5
|
+
required: [
|
|
6
|
+
"context",
|
|
7
|
+
"message"
|
|
8
|
+
],
|
|
9
|
+
properties: {
|
|
10
|
+
owner: {
|
|
11
|
+
// don't use "owned: true", we want it this way
|
|
12
|
+
$ref: "user",
|
|
13
|
+
noForm: true
|
|
14
|
+
},
|
|
15
|
+
context: {
|
|
16
|
+
type: "string"
|
|
17
|
+
},
|
|
18
|
+
message: {
|
|
19
|
+
type: "string"
|
|
20
|
+
},
|
|
21
|
+
details: {
|
|
22
|
+
type: "object",
|
|
23
|
+
variable: true
|
|
24
|
+
},
|
|
25
|
+
created_at: {
|
|
26
|
+
type: "string",
|
|
27
|
+
format: "date-time"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
icon: "magnifying-glass",
|
|
31
|
+
presets: [
|
|
32
|
+
"view"
|
|
33
|
+
],
|
|
34
|
+
filters: [
|
|
35
|
+
"context",
|
|
36
|
+
"message",
|
|
37
|
+
"owner"
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
functions: {
|
|
41
|
+
get: get,
|
|
42
|
+
getAll: getAll,
|
|
43
|
+
insert: insert
|
|
44
|
+
}
|
|
45
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export declare const resourceUsage: Omit<import("@aeriajs/types").Collection<{
|
|
2
|
+
description: {
|
|
3
|
+
readonly $id: "resourceUsage";
|
|
4
|
+
readonly required: readonly [];
|
|
5
|
+
readonly properties: {
|
|
6
|
+
readonly hits: {
|
|
7
|
+
readonly type: "integer";
|
|
8
|
+
};
|
|
9
|
+
readonly last_maximum_reach: {
|
|
10
|
+
readonly type: "string";
|
|
11
|
+
readonly format: "date-time";
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}>, "description" | "functions" | "item"> & {
|
|
16
|
+
item: import("@aeriajs/types").SchemaWithId<{
|
|
17
|
+
readonly $id: "resourceUsage";
|
|
18
|
+
readonly required: readonly [];
|
|
19
|
+
readonly properties: {
|
|
20
|
+
readonly hits: {
|
|
21
|
+
readonly type: "integer";
|
|
22
|
+
};
|
|
23
|
+
readonly last_maximum_reach: {
|
|
24
|
+
readonly type: "string";
|
|
25
|
+
readonly format: "date-time";
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
}>;
|
|
29
|
+
description: {
|
|
30
|
+
readonly $id: "resourceUsage";
|
|
31
|
+
readonly required: readonly [];
|
|
32
|
+
readonly properties: {
|
|
33
|
+
readonly hits: {
|
|
34
|
+
readonly type: "integer";
|
|
35
|
+
};
|
|
36
|
+
readonly last_maximum_reach: {
|
|
37
|
+
readonly type: "string";
|
|
38
|
+
readonly format: "date-time";
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
functions: {
|
|
43
|
+
[P: string]: (payload: any, context: import("@aeriajs/types").Context<{
|
|
44
|
+
readonly $id: "resourceUsage";
|
|
45
|
+
readonly required: readonly [];
|
|
46
|
+
readonly properties: {
|
|
47
|
+
readonly hits: {
|
|
48
|
+
readonly type: "integer";
|
|
49
|
+
};
|
|
50
|
+
readonly last_maximum_reach: {
|
|
51
|
+
readonly type: "string";
|
|
52
|
+
readonly format: "date-time";
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
}>, ...args: any[]) => any;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resourceUsage = void 0;
|
|
4
|
+
const api_1 = require("@aeriajs/api");
|
|
5
|
+
exports.resourceUsage = (0, api_1.defineCollection)({
|
|
6
|
+
description: {
|
|
7
|
+
$id: 'resourceUsage',
|
|
8
|
+
required: [],
|
|
9
|
+
properties: {
|
|
10
|
+
hits: {
|
|
11
|
+
type: 'integer',
|
|
12
|
+
},
|
|
13
|
+
last_maximum_reach: {
|
|
14
|
+
type: 'string',
|
|
15
|
+
format: 'date-time',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { defineCollection } from "@aeriajs/api";
|
|
2
|
+
export var resourceUsage = defineCollection({
|
|
3
|
+
description: {
|
|
4
|
+
$id: "resourceUsage",
|
|
5
|
+
required: [],
|
|
6
|
+
properties: {
|
|
7
|
+
hits: {
|
|
8
|
+
type: "integer"
|
|
9
|
+
},
|
|
10
|
+
last_maximum_reach: {
|
|
11
|
+
type: "string",
|
|
12
|
+
format: "date-time"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Context } from '@aeriajs/types';
|
|
2
|
+
import type { description } from './description.js';
|
|
3
|
+
export declare enum ActivationErrors {
|
|
4
|
+
UserNotFound = "USER_NOT_FOUND",
|
|
5
|
+
AlreadyActiveUser = "ALREADY_ACTIVE_USER",
|
|
6
|
+
InvalidLink = "INVALID_LINK"
|
|
7
|
+
}
|
|
8
|
+
export declare const activate: (payload: {
|
|
9
|
+
password: string;
|
|
10
|
+
}, context: Context<typeof description>) => Promise<import("@aeriajs/types").GenericResponse | import("@aeriajs/types").Left<ActivationErrors.InvalidLink> | import("@aeriajs/types").Left<ActivationErrors.UserNotFound> | import("@aeriajs/types").Left<ActivationErrors.AlreadyActiveUser> | import("@aeriajs/types").Right<true>>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.activate = exports.ActivationErrors = void 0;
|
|
7
|
+
const api_1 = require("@aeriajs/api");
|
|
8
|
+
const common_1 = require("@aeriajs/common");
|
|
9
|
+
const bcrypt_1 = __importDefault(require("bcrypt"));
|
|
10
|
+
var ActivationErrors;
|
|
11
|
+
(function (ActivationErrors) {
|
|
12
|
+
ActivationErrors["UserNotFound"] = "USER_NOT_FOUND";
|
|
13
|
+
ActivationErrors["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
|
|
14
|
+
ActivationErrors["InvalidLink"] = "INVALID_LINK";
|
|
15
|
+
})(ActivationErrors || (exports.ActivationErrors = ActivationErrors = {}));
|
|
16
|
+
const activate = async (payload, context) => {
|
|
17
|
+
const { u: userId, t: token, } = context.request.query;
|
|
18
|
+
if (!userId || !token) {
|
|
19
|
+
return (0, common_1.left)(ActivationErrors.InvalidLink);
|
|
20
|
+
}
|
|
21
|
+
const user = await context.collection.model.findOne({
|
|
22
|
+
_id: new api_1.ObjectId(userId),
|
|
23
|
+
}, {
|
|
24
|
+
projection: {
|
|
25
|
+
password: 1,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
if (!user) {
|
|
29
|
+
return (0, common_1.left)(ActivationErrors.UserNotFound);
|
|
30
|
+
}
|
|
31
|
+
if (user.active) {
|
|
32
|
+
return (0, common_1.left)(ActivationErrors.AlreadyActiveUser);
|
|
33
|
+
}
|
|
34
|
+
const equal = await bcrypt_1.default.compare(user._id.toString(), token);
|
|
35
|
+
if (!equal) {
|
|
36
|
+
return (0, common_1.left)(ActivationErrors.InvalidLink);
|
|
37
|
+
}
|
|
38
|
+
if (!user.password) {
|
|
39
|
+
if (!payload.password) {
|
|
40
|
+
return context.response.writeHead(302, {
|
|
41
|
+
location: `/user/activation?step=password&u=${userId}&t=${token}`,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
await context.collection.model.updateOne({
|
|
45
|
+
_id: user._id,
|
|
46
|
+
}, {
|
|
47
|
+
$set: {
|
|
48
|
+
active: true,
|
|
49
|
+
password: await bcrypt_1.default.hash(payload.password, 10),
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
return (0, common_1.right)(true);
|
|
53
|
+
}
|
|
54
|
+
await context.collection.model.updateOne({
|
|
55
|
+
_id: user._id,
|
|
56
|
+
}, {
|
|
57
|
+
$set: {
|
|
58
|
+
active: true,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
return context.response.writeHead(302, {
|
|
62
|
+
location: '/user/activation',
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
exports.activate = activate;
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
+
try {
|
|
3
|
+
var info = gen[key](arg);
|
|
4
|
+
var value = info.value;
|
|
5
|
+
} catch (error) {
|
|
6
|
+
reject(error);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (info.done) {
|
|
10
|
+
resolve(value);
|
|
11
|
+
} else {
|
|
12
|
+
Promise.resolve(value).then(_next, _throw);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function _async_to_generator(fn) {
|
|
16
|
+
return function() {
|
|
17
|
+
var self = this, args = arguments;
|
|
18
|
+
return new Promise(function(resolve, reject) {
|
|
19
|
+
var gen = fn.apply(self, args);
|
|
20
|
+
function _next(value) {
|
|
21
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
+
}
|
|
23
|
+
function _throw(err) {
|
|
24
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
+
}
|
|
26
|
+
_next(undefined);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function _ts_generator(thisArg, body) {
|
|
31
|
+
var f, y, t, g, _ = {
|
|
32
|
+
label: 0,
|
|
33
|
+
sent: function() {
|
|
34
|
+
if (t[0] & 1) throw t[1];
|
|
35
|
+
return t[1];
|
|
36
|
+
},
|
|
37
|
+
trys: [],
|
|
38
|
+
ops: []
|
|
39
|
+
};
|
|
40
|
+
return g = {
|
|
41
|
+
next: verb(0),
|
|
42
|
+
"throw": verb(1),
|
|
43
|
+
"return": verb(2)
|
|
44
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
45
|
+
return this;
|
|
46
|
+
}), g;
|
|
47
|
+
function verb(n) {
|
|
48
|
+
return function(v) {
|
|
49
|
+
return step([
|
|
50
|
+
n,
|
|
51
|
+
v
|
|
52
|
+
]);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function step(op) {
|
|
56
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
57
|
+
while(_)try {
|
|
58
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
59
|
+
if (y = 0, t) op = [
|
|
60
|
+
op[0] & 2,
|
|
61
|
+
t.value
|
|
62
|
+
];
|
|
63
|
+
switch(op[0]){
|
|
64
|
+
case 0:
|
|
65
|
+
case 1:
|
|
66
|
+
t = op;
|
|
67
|
+
break;
|
|
68
|
+
case 4:
|
|
69
|
+
_.label++;
|
|
70
|
+
return {
|
|
71
|
+
value: op[1],
|
|
72
|
+
done: false
|
|
73
|
+
};
|
|
74
|
+
case 5:
|
|
75
|
+
_.label++;
|
|
76
|
+
y = op[1];
|
|
77
|
+
op = [
|
|
78
|
+
0
|
|
79
|
+
];
|
|
80
|
+
continue;
|
|
81
|
+
case 7:
|
|
82
|
+
op = _.ops.pop();
|
|
83
|
+
_.trys.pop();
|
|
84
|
+
continue;
|
|
85
|
+
default:
|
|
86
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
87
|
+
_ = 0;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
91
|
+
_.label = op[1];
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
95
|
+
_.label = t[1];
|
|
96
|
+
t = op;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (t && _.label < t[2]) {
|
|
100
|
+
_.label = t[2];
|
|
101
|
+
_.ops.push(op);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
if (t[2]) _.ops.pop();
|
|
105
|
+
_.trys.pop();
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
op = body.call(thisArg, _);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
op = [
|
|
111
|
+
6,
|
|
112
|
+
e
|
|
113
|
+
];
|
|
114
|
+
y = 0;
|
|
115
|
+
} finally{
|
|
116
|
+
f = t = 0;
|
|
117
|
+
}
|
|
118
|
+
if (op[0] & 5) throw op[1];
|
|
119
|
+
return {
|
|
120
|
+
value: op[0] ? op[1] : void 0,
|
|
121
|
+
done: true
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
import { ObjectId } from "@aeriajs/api";
|
|
126
|
+
import { left, right } from "@aeriajs/common";
|
|
127
|
+
import bcrypt from "bcrypt";
|
|
128
|
+
export var ActivationErrors;
|
|
129
|
+
(function(ActivationErrors) {
|
|
130
|
+
ActivationErrors["UserNotFound"] = "USER_NOT_FOUND";
|
|
131
|
+
ActivationErrors["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
|
|
132
|
+
ActivationErrors["InvalidLink"] = "INVALID_LINK";
|
|
133
|
+
})(ActivationErrors || (ActivationErrors = {}));
|
|
134
|
+
export var activate = function() {
|
|
135
|
+
var _ref = _async_to_generator(function(payload, context) {
|
|
136
|
+
var _context_request_query, userId, token, user, equal, _, _1, _tmp, _tmp1, _tmp2;
|
|
137
|
+
return _ts_generator(this, function(_state) {
|
|
138
|
+
switch(_state.label){
|
|
139
|
+
case 0:
|
|
140
|
+
_context_request_query = context.request.query, userId = _context_request_query.u, token = _context_request_query.t;
|
|
141
|
+
if (!userId || !token) {
|
|
142
|
+
return [
|
|
143
|
+
2,
|
|
144
|
+
left("INVALID_LINK")
|
|
145
|
+
];
|
|
146
|
+
}
|
|
147
|
+
return [
|
|
148
|
+
4,
|
|
149
|
+
context.collection.model.findOne({
|
|
150
|
+
_id: new ObjectId(userId)
|
|
151
|
+
}, {
|
|
152
|
+
projection: {
|
|
153
|
+
password: 1
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
];
|
|
157
|
+
case 1:
|
|
158
|
+
user = _state.sent();
|
|
159
|
+
if (!user) {
|
|
160
|
+
return [
|
|
161
|
+
2,
|
|
162
|
+
left("USER_NOT_FOUND")
|
|
163
|
+
];
|
|
164
|
+
}
|
|
165
|
+
if (user.active) {
|
|
166
|
+
return [
|
|
167
|
+
2,
|
|
168
|
+
left("ALREADY_ACTIVE_USER")
|
|
169
|
+
];
|
|
170
|
+
}
|
|
171
|
+
return [
|
|
172
|
+
4,
|
|
173
|
+
bcrypt.compare(user._id.toString(), token)
|
|
174
|
+
];
|
|
175
|
+
case 2:
|
|
176
|
+
equal = _state.sent();
|
|
177
|
+
if (!equal) {
|
|
178
|
+
return [
|
|
179
|
+
2,
|
|
180
|
+
left("INVALID_LINK")
|
|
181
|
+
];
|
|
182
|
+
}
|
|
183
|
+
if (!!user.password) return [
|
|
184
|
+
3,
|
|
185
|
+
5
|
|
186
|
+
];
|
|
187
|
+
if (!payload.password) {
|
|
188
|
+
return [
|
|
189
|
+
2,
|
|
190
|
+
context.response.writeHead(302, {
|
|
191
|
+
location: "/user/activation?step=password&u=".concat(userId, "&t=").concat(token)
|
|
192
|
+
})
|
|
193
|
+
];
|
|
194
|
+
}
|
|
195
|
+
_1 = (_ = context.collection.model).updateOne;
|
|
196
|
+
_tmp = [
|
|
197
|
+
{
|
|
198
|
+
_id: user._id
|
|
199
|
+
}
|
|
200
|
+
];
|
|
201
|
+
_tmp1 = {};
|
|
202
|
+
_tmp2 = {
|
|
203
|
+
active: true
|
|
204
|
+
};
|
|
205
|
+
return [
|
|
206
|
+
4,
|
|
207
|
+
bcrypt.hash(payload.password, 10)
|
|
208
|
+
];
|
|
209
|
+
case 3:
|
|
210
|
+
return [
|
|
211
|
+
4,
|
|
212
|
+
_1.apply(_, _tmp.concat([
|
|
213
|
+
(_tmp1.$set = (_tmp2.password = _state.sent(), _tmp2), _tmp1)
|
|
214
|
+
]))
|
|
215
|
+
];
|
|
216
|
+
case 4:
|
|
217
|
+
_state.sent();
|
|
218
|
+
return [
|
|
219
|
+
2,
|
|
220
|
+
right(true)
|
|
221
|
+
];
|
|
222
|
+
case 5:
|
|
223
|
+
return [
|
|
224
|
+
4,
|
|
225
|
+
context.collection.model.updateOne({
|
|
226
|
+
_id: user._id
|
|
227
|
+
}, {
|
|
228
|
+
$set: {
|
|
229
|
+
active: true
|
|
230
|
+
}
|
|
231
|
+
})
|
|
232
|
+
];
|
|
233
|
+
case 6:
|
|
234
|
+
_state.sent();
|
|
235
|
+
return [
|
|
236
|
+
2,
|
|
237
|
+
context.response.writeHead(302, {
|
|
238
|
+
location: "/user/activation"
|
|
239
|
+
})
|
|
240
|
+
];
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
return function activate(payload, context) {
|
|
245
|
+
return _ref.apply(this, arguments);
|
|
246
|
+
};
|
|
247
|
+
}();
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Context, SchemaWithId, ObjectId } from '@aeriajs/types';
|
|
2
|
+
import type { description } from './description.js';
|
|
3
|
+
type Props = {
|
|
4
|
+
email: string;
|
|
5
|
+
password: string;
|
|
6
|
+
} | {
|
|
7
|
+
revalidate: true;
|
|
8
|
+
};
|
|
9
|
+
type Return = {
|
|
10
|
+
user: Pick<SchemaWithId<typeof description>, 'name' | 'email' | 'roles' | 'active'> & {
|
|
11
|
+
_id: ObjectId | null;
|
|
12
|
+
};
|
|
13
|
+
token: {
|
|
14
|
+
type: 'bearer';
|
|
15
|
+
content: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare enum AuthenticationErrors {
|
|
19
|
+
Unauthenticated = "UNAUTHENTICATED",
|
|
20
|
+
InvalidCredentials = "INVALID_CREDENTIALS",
|
|
21
|
+
InactiveUser = "INACTIVE_USER"
|
|
22
|
+
}
|
|
23
|
+
export declare const authenticate: (props: Props, context: Context<typeof description>) => Promise<import("@aeriajs/types").Right<Return> | import("@aeriajs/types").Left<AuthenticationErrors.Unauthenticated> | import("@aeriajs/types").Left<AuthenticationErrors.InvalidCredentials> | import("@aeriajs/types").Left<AuthenticationErrors.InactiveUser>>;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.authenticate = exports.AuthenticationErrors = void 0;
|
|
4
|
+
const bcrypt_1 = require("bcrypt");
|
|
5
|
+
const api_1 = require("@aeriajs/api");
|
|
6
|
+
const common_1 = require("@aeriajs/common");
|
|
7
|
+
var AuthenticationErrors;
|
|
8
|
+
(function (AuthenticationErrors) {
|
|
9
|
+
AuthenticationErrors["Unauthenticated"] = "UNAUTHENTICATED";
|
|
10
|
+
AuthenticationErrors["InvalidCredentials"] = "INVALID_CREDENTIALS";
|
|
11
|
+
AuthenticationErrors["InactiveUser"] = "INACTIVE_USER";
|
|
12
|
+
})(AuthenticationErrors || (exports.AuthenticationErrors = AuthenticationErrors = {}));
|
|
13
|
+
const getUser = async (userId, context) => {
|
|
14
|
+
const leanUser = await context.collection.functions.get({
|
|
15
|
+
filters: {
|
|
16
|
+
_id: userId,
|
|
17
|
+
},
|
|
18
|
+
populate: ['picture_file'],
|
|
19
|
+
});
|
|
20
|
+
if (!leanUser) {
|
|
21
|
+
throw new Error();
|
|
22
|
+
}
|
|
23
|
+
const tokenContent = {
|
|
24
|
+
sub: leanUser._id,
|
|
25
|
+
roles: leanUser.roles,
|
|
26
|
+
userinfo: {},
|
|
27
|
+
};
|
|
28
|
+
if (context.config.logSuccessfulAuthentications) {
|
|
29
|
+
await context.log('successful authentication', {
|
|
30
|
+
email: leanUser.email,
|
|
31
|
+
roles: leanUser.roles,
|
|
32
|
+
_id: leanUser._id,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
if (context.config.tokenUserProperties) {
|
|
36
|
+
const pick = (obj, properties) => properties.reduce((a, prop) => {
|
|
37
|
+
if ('prop' in obj) {
|
|
38
|
+
return a;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
...a,
|
|
42
|
+
[prop]: obj[prop],
|
|
43
|
+
};
|
|
44
|
+
}, {});
|
|
45
|
+
tokenContent.userinfo = pick(leanUser, context.config.tokenUserProperties);
|
|
46
|
+
}
|
|
47
|
+
const token = await (0, api_1.signToken)(tokenContent);
|
|
48
|
+
return {
|
|
49
|
+
user: leanUser,
|
|
50
|
+
token: {
|
|
51
|
+
type: 'bearer',
|
|
52
|
+
content: token,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
const authenticate = async (props, context) => {
|
|
57
|
+
if ('revalidate' in props) {
|
|
58
|
+
return context.token.authenticated
|
|
59
|
+
? (0, common_1.right)(await getUser(context.token.sub, context))
|
|
60
|
+
: (0, common_1.left)(AuthenticationErrors.Unauthenticated);
|
|
61
|
+
}
|
|
62
|
+
if (typeof props.email !== 'string') {
|
|
63
|
+
return (0, common_1.left)(AuthenticationErrors.InvalidCredentials);
|
|
64
|
+
}
|
|
65
|
+
if (context.config.defaultUser) {
|
|
66
|
+
if (props.email === context.config.defaultUser.username && props.password === context.config.defaultUser.password) {
|
|
67
|
+
const token = await (0, api_1.signToken)({
|
|
68
|
+
_id: null,
|
|
69
|
+
roles: ['root'],
|
|
70
|
+
});
|
|
71
|
+
return (0, common_1.right)({
|
|
72
|
+
user: {
|
|
73
|
+
_id: null,
|
|
74
|
+
name: 'God Mode',
|
|
75
|
+
email: '',
|
|
76
|
+
roles: ['root'],
|
|
77
|
+
active: true,
|
|
78
|
+
},
|
|
79
|
+
token: {
|
|
80
|
+
type: 'bearer',
|
|
81
|
+
content: token,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const user = await context.collection.model.findOne({
|
|
87
|
+
email: props.email,
|
|
88
|
+
}, {
|
|
89
|
+
projection: {
|
|
90
|
+
email: 1,
|
|
91
|
+
password: 1,
|
|
92
|
+
active: 1,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
if (!user || !user.password || !await (0, bcrypt_1.compare)(props.password, user.password)) {
|
|
96
|
+
return (0, common_1.left)(AuthenticationErrors.InvalidCredentials);
|
|
97
|
+
}
|
|
98
|
+
if (!user.active) {
|
|
99
|
+
return (0, common_1.left)(AuthenticationErrors.InactiveUser);
|
|
100
|
+
}
|
|
101
|
+
return (0, common_1.right)(await getUser(user._id, context));
|
|
102
|
+
};
|
|
103
|
+
exports.authenticate = authenticate;
|