@nocobase/plugin-users 0.16.0-alpha.5 → 0.17.0-alpha.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.
- package/dist/externalVersion.js +5 -7
- package/dist/server/actions/users.d.ts +0 -8
- package/dist/server/actions/users.js +0 -147
- package/dist/server/server.d.ts +0 -7
- package/dist/server/server.js +2 -83
- package/package.json +2 -2
- package/dist/server/authenticators/index.d.ts +0 -7
- package/dist/server/authenticators/index.js +0 -46
- package/dist/server/authenticators/password.d.ts +0 -2
- package/dist/server/authenticators/password.js +0 -48
- package/dist/server/jwt-service.d.ts +0 -12
- package/dist/server/jwt-service.js +0 -61
- package/dist/server/middlewares/check.d.ts +0 -1
- package/dist/server/middlewares/check.js +0 -35
- package/dist/server/middlewares/index.d.ts +0 -2
- package/dist/server/middlewares/index.js +0 -30
- package/dist/server/middlewares/parseToken.d.ts +0 -2
- package/dist/server/middlewares/parseToken.js +0 -62
package/dist/externalVersion.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
"@nocobase/client": "0.
|
|
3
|
-
"
|
|
4
|
-
"@nocobase/
|
|
5
|
-
"@nocobase/
|
|
6
|
-
"@nocobase/
|
|
7
|
-
"@nocobase/utils": "0.16.0-alpha.5",
|
|
8
|
-
"@nocobase/actions": "0.16.0-alpha.5"
|
|
2
|
+
"@nocobase/client": "0.17.0-alpha.1",
|
|
3
|
+
"@nocobase/database": "0.17.0-alpha.1",
|
|
4
|
+
"@nocobase/server": "0.17.0-alpha.1",
|
|
5
|
+
"@nocobase/utils": "0.17.0-alpha.1",
|
|
6
|
+
"@nocobase/actions": "0.17.0-alpha.1"
|
|
9
7
|
};
|
|
@@ -1,10 +1,2 @@
|
|
|
1
1
|
import { Context, Next } from '@nocobase/actions';
|
|
2
|
-
export declare function check(ctx: Context, next: Next): Promise<void>;
|
|
3
|
-
export declare function signin(ctx: Context, next: Next): Promise<any>;
|
|
4
|
-
export declare function signout(ctx: Context, next: Next): Promise<void>;
|
|
5
|
-
export declare function signup(ctx: Context, next: Next): Promise<void>;
|
|
6
|
-
export declare function lostpassword(ctx: Context, next: Next): Promise<void>;
|
|
7
|
-
export declare function resetpassword(ctx: Context, next: Next): Promise<void>;
|
|
8
|
-
export declare function getUserByResetToken(ctx: Context, next: Next): Promise<void>;
|
|
9
2
|
export declare function updateProfile(ctx: Context, next: Next): Promise<void>;
|
|
10
|
-
export declare function changePassword(ctx: Context, next: Next): Promise<void>;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
1
|
var __defProp = Object.defineProperty;
|
|
3
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
5
|
var __export = (target, all) => {
|
|
8
6
|
for (var name in all)
|
|
@@ -16,126 +14,12 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
14
|
}
|
|
17
15
|
return to;
|
|
18
16
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
18
|
var users_exports = {};
|
|
29
19
|
__export(users_exports, {
|
|
30
|
-
changePassword: () => changePassword,
|
|
31
|
-
check: () => check,
|
|
32
|
-
getUserByResetToken: () => getUserByResetToken,
|
|
33
|
-
lostpassword: () => lostpassword,
|
|
34
|
-
resetpassword: () => resetpassword,
|
|
35
|
-
signin: () => signin,
|
|
36
|
-
signout: () => signout,
|
|
37
|
-
signup: () => signup,
|
|
38
20
|
updateProfile: () => updateProfile
|
|
39
21
|
});
|
|
40
22
|
module.exports = __toCommonJS(users_exports);
|
|
41
|
-
var import_resourcer = require("@nocobase/resourcer");
|
|
42
|
-
var import_crypto = __toESM(require("crypto"));
|
|
43
|
-
var import__ = require("../");
|
|
44
|
-
async function check(ctx, next) {
|
|
45
|
-
if (ctx.state.currentUser) {
|
|
46
|
-
const user = ctx.state.currentUser.toJSON();
|
|
47
|
-
ctx.body = user;
|
|
48
|
-
} else {
|
|
49
|
-
ctx.body = {};
|
|
50
|
-
}
|
|
51
|
-
await next();
|
|
52
|
-
}
|
|
53
|
-
async function signin(ctx, next) {
|
|
54
|
-
const { authenticators, jwtService } = ctx.app.getPlugin("users");
|
|
55
|
-
const branches = {};
|
|
56
|
-
for (const [name, authenticator] of authenticators.getEntities()) {
|
|
57
|
-
branches[name] = authenticator;
|
|
58
|
-
}
|
|
59
|
-
return (0, import_resourcer.branch)(branches, (context) => context.action.params.authenticator ?? "password")(ctx, () => {
|
|
60
|
-
const user = ctx.state.currentUser.toJSON();
|
|
61
|
-
const token = jwtService.sign({ userId: user.id });
|
|
62
|
-
ctx.body = {
|
|
63
|
-
user,
|
|
64
|
-
token
|
|
65
|
-
};
|
|
66
|
-
return next();
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
async function signout(ctx, next) {
|
|
70
|
-
ctx.body = ctx.state.currentUser;
|
|
71
|
-
await next();
|
|
72
|
-
}
|
|
73
|
-
async function signup(ctx, next) {
|
|
74
|
-
const User = ctx.db.getRepository("users");
|
|
75
|
-
const { values } = ctx.action.params;
|
|
76
|
-
const user = await User.create({ values });
|
|
77
|
-
ctx.body = user;
|
|
78
|
-
await next();
|
|
79
|
-
}
|
|
80
|
-
async function lostpassword(ctx, next) {
|
|
81
|
-
const {
|
|
82
|
-
values: { email }
|
|
83
|
-
} = ctx.action.params;
|
|
84
|
-
if (!email) {
|
|
85
|
-
ctx.throw(400, { code: "InvalidUserData", message: ctx.t("Please fill in your email address", { ns: import__.namespace }) });
|
|
86
|
-
}
|
|
87
|
-
const User = ctx.db.getCollection("users");
|
|
88
|
-
const user = await User.model.findOne({
|
|
89
|
-
where: {
|
|
90
|
-
email
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
if (!user) {
|
|
94
|
-
ctx.throw(404, {
|
|
95
|
-
code: "InvalidUserData",
|
|
96
|
-
message: ctx.t("The email is incorrect, please re-enter", { ns: import__.namespace })
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
user.resetToken = import_crypto.default.randomBytes(20).toString("hex");
|
|
100
|
-
await user.save();
|
|
101
|
-
ctx.body = user;
|
|
102
|
-
await next();
|
|
103
|
-
}
|
|
104
|
-
async function resetpassword(ctx, next) {
|
|
105
|
-
const {
|
|
106
|
-
values: { email, password, resetToken }
|
|
107
|
-
} = ctx.action.params;
|
|
108
|
-
const User = ctx.db.getCollection("users");
|
|
109
|
-
const user = await User.model.findOne({
|
|
110
|
-
where: {
|
|
111
|
-
email,
|
|
112
|
-
resetToken
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
if (!user) {
|
|
116
|
-
ctx.throw(404);
|
|
117
|
-
}
|
|
118
|
-
user.token = null;
|
|
119
|
-
user.resetToken = null;
|
|
120
|
-
user.password = password;
|
|
121
|
-
await user.save();
|
|
122
|
-
ctx.body = user;
|
|
123
|
-
await next();
|
|
124
|
-
}
|
|
125
|
-
async function getUserByResetToken(ctx, next) {
|
|
126
|
-
const { token } = ctx.action.params;
|
|
127
|
-
const User = ctx.db.getCollection("users");
|
|
128
|
-
const user = await User.model.findOne({
|
|
129
|
-
where: {
|
|
130
|
-
resetToken: token
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
if (!user) {
|
|
134
|
-
ctx.throw(401);
|
|
135
|
-
}
|
|
136
|
-
ctx.body = user;
|
|
137
|
-
await next();
|
|
138
|
-
}
|
|
139
23
|
async function updateProfile(ctx, next) {
|
|
140
24
|
const { values } = ctx.action.params;
|
|
141
25
|
const { currentUser } = ctx.state;
|
|
@@ -150,38 +34,7 @@ async function updateProfile(ctx, next) {
|
|
|
150
34
|
ctx.body = result;
|
|
151
35
|
await next();
|
|
152
36
|
}
|
|
153
|
-
async function changePassword(ctx, next) {
|
|
154
|
-
const {
|
|
155
|
-
values: { oldPassword, newPassword }
|
|
156
|
-
} = ctx.action.params;
|
|
157
|
-
if (!ctx.state.currentUser) {
|
|
158
|
-
ctx.throw(401);
|
|
159
|
-
}
|
|
160
|
-
const User = ctx.db.getCollection("users");
|
|
161
|
-
const user = await User.model.findOne({
|
|
162
|
-
where: {
|
|
163
|
-
email: ctx.state.currentUser.email
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
const pwd = User.getField("password");
|
|
167
|
-
const isValid = await pwd.verify(oldPassword, user.password);
|
|
168
|
-
if (!isValid) {
|
|
169
|
-
ctx.throw(401, ctx.t("The password is incorrect, please re-enter", { ns: import__.namespace }));
|
|
170
|
-
}
|
|
171
|
-
user.password = newPassword;
|
|
172
|
-
user.save();
|
|
173
|
-
ctx.body = ctx.state.currentUser.toJSON();
|
|
174
|
-
await next();
|
|
175
|
-
}
|
|
176
37
|
// Annotate the CommonJS export names for ESM import in node:
|
|
177
38
|
0 && (module.exports = {
|
|
178
|
-
changePassword,
|
|
179
|
-
check,
|
|
180
|
-
getUserByResetToken,
|
|
181
|
-
lostpassword,
|
|
182
|
-
resetpassword,
|
|
183
|
-
signin,
|
|
184
|
-
signout,
|
|
185
|
-
signup,
|
|
186
39
|
updateProfile
|
|
187
40
|
});
|
package/dist/server/server.d.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import { HandlerType } from '@nocobase/resourcer';
|
|
2
1
|
import { Plugin } from '@nocobase/server';
|
|
3
|
-
import { Registry } from '@nocobase/utils';
|
|
4
|
-
import { JwtOptions, JwtService } from './jwt-service';
|
|
5
2
|
export interface UserPluginConfig {
|
|
6
3
|
name?: string;
|
|
7
|
-
jwt: JwtOptions;
|
|
8
4
|
}
|
|
9
5
|
export default class UsersPlugin extends Plugin<UserPluginConfig> {
|
|
10
|
-
jwtService: JwtService;
|
|
11
|
-
authenticators: Registry<HandlerType>;
|
|
12
6
|
constructor(app: any, options: any);
|
|
13
7
|
beforeLoad(): Promise<void>;
|
|
14
8
|
load(): Promise<void>;
|
|
@@ -19,5 +13,4 @@ export default class UsersPlugin extends Plugin<UserPluginConfig> {
|
|
|
19
13
|
rootUsername: any;
|
|
20
14
|
};
|
|
21
15
|
install(options: any): Promise<void>;
|
|
22
|
-
initVerification(): Promise<void>;
|
|
23
16
|
}
|
package/dist/server/server.js
CHANGED
|
@@ -36,15 +36,10 @@ var import_utils = require("@nocobase/utils");
|
|
|
36
36
|
var import_path = require("path");
|
|
37
37
|
var import__ = require("./");
|
|
38
38
|
var actions = __toESM(require("./actions/users"));
|
|
39
|
-
var import_authenticators = __toESM(require("./authenticators"));
|
|
40
|
-
var import_jwt_service = require("./jwt-service");
|
|
41
39
|
var import_locale = require("./locale");
|
|
42
40
|
class UsersPlugin extends import_server.Plugin {
|
|
43
|
-
jwtService;
|
|
44
|
-
authenticators = new import_utils.Registry();
|
|
45
41
|
constructor(app, options) {
|
|
46
42
|
super(app, options);
|
|
47
|
-
this.jwtService = new import_jwt_service.JwtService((options == null ? void 0 : options.jwt) || {});
|
|
48
43
|
}
|
|
49
44
|
async beforeLoad() {
|
|
50
45
|
this.app.i18n.addResources("zh-CN", import__.namespace, import_locale.zhCN);
|
|
@@ -128,11 +123,8 @@ class UsersPlugin extends import_server.Plugin {
|
|
|
128
123
|
}
|
|
129
124
|
};
|
|
130
125
|
});
|
|
131
|
-
const
|
|
132
|
-
const loggedInActions = ["signout", "updateProfile", "changePassword"];
|
|
133
|
-
publicActions.forEach((action) => this.app.acl.allow("users", action));
|
|
126
|
+
const loggedInActions = ["updateProfile"];
|
|
134
127
|
loggedInActions.forEach((action) => this.app.acl.allow("users", action, "loggedIn"));
|
|
135
|
-
this.app.on("beforeStart", () => this.initVerification());
|
|
136
128
|
}
|
|
137
129
|
async load() {
|
|
138
130
|
await this.db.import({
|
|
@@ -145,7 +137,6 @@ class UsersPlugin extends import_server.Plugin {
|
|
|
145
137
|
plugin: this
|
|
146
138
|
}
|
|
147
139
|
});
|
|
148
|
-
(0, import_authenticators.default)(this);
|
|
149
140
|
}
|
|
150
141
|
getInstallingData(options = {}) {
|
|
151
142
|
var _a;
|
|
@@ -169,7 +160,7 @@ class UsersPlugin extends import_server.Plugin {
|
|
|
169
160
|
if (await User.repository.findOne({ filter: { email: rootEmail } })) {
|
|
170
161
|
return;
|
|
171
162
|
}
|
|
172
|
-
|
|
163
|
+
await User.repository.create({
|
|
173
164
|
values: {
|
|
174
165
|
email: rootEmail,
|
|
175
166
|
password: rootPassword,
|
|
@@ -182,76 +173,4 @@ class UsersPlugin extends import_server.Plugin {
|
|
|
182
173
|
await repo.db2cm("users");
|
|
183
174
|
}
|
|
184
175
|
}
|
|
185
|
-
// TODO(module): should move to preset or dynamic configuration panel
|
|
186
|
-
async initVerification() {
|
|
187
|
-
const verificationPlugin = this.app.getPlugin("verification");
|
|
188
|
-
if (!verificationPlugin) {
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const systemSettingsRepo = this.db.getRepository("systemSettings");
|
|
192
|
-
const settings = await systemSettingsRepo.findOne();
|
|
193
|
-
if (!settings.smsAuthEnabled) {
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
verificationPlugin.interceptors.register("users:signin", {
|
|
197
|
-
manual: true,
|
|
198
|
-
getReceiver(ctx) {
|
|
199
|
-
return ctx.action.params.values.phone;
|
|
200
|
-
},
|
|
201
|
-
expiresIn: 120,
|
|
202
|
-
validate: async (ctx, phone) => {
|
|
203
|
-
if (!phone) {
|
|
204
|
-
throw new Error(ctx.t("Not a valid cellphone number, please re-enter"));
|
|
205
|
-
}
|
|
206
|
-
const User = this.db.getCollection("users");
|
|
207
|
-
const exists = await User.model.count({
|
|
208
|
-
where: {
|
|
209
|
-
phone
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
if (!exists) {
|
|
213
|
-
throw new Error(ctx.t("The phone number is not registered, please register first", { ns: import__.namespace }));
|
|
214
|
-
}
|
|
215
|
-
return true;
|
|
216
|
-
}
|
|
217
|
-
});
|
|
218
|
-
verificationPlugin.interceptors.register("users:signup", {
|
|
219
|
-
getReceiver(ctx) {
|
|
220
|
-
return ctx.action.params.values.phone;
|
|
221
|
-
},
|
|
222
|
-
expiresIn: 120,
|
|
223
|
-
validate: async (ctx, phone) => {
|
|
224
|
-
if (!phone) {
|
|
225
|
-
throw new Error(ctx.t("Not a valid cellphone number, please re-enter", { ns: import__.namespace }));
|
|
226
|
-
}
|
|
227
|
-
const User = this.db.getCollection("users");
|
|
228
|
-
const exists = await User.model.count({
|
|
229
|
-
where: {
|
|
230
|
-
phone
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
if (exists) {
|
|
234
|
-
throw new Error(ctx.t("The phone number has been registered, please login directly", { ns: import__.namespace }));
|
|
235
|
-
}
|
|
236
|
-
return true;
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
this.authenticators.register(
|
|
240
|
-
"sms",
|
|
241
|
-
(ctx, next) => verificationPlugin.intercept(ctx, async () => {
|
|
242
|
-
const { values } = ctx.action.params;
|
|
243
|
-
const User = ctx.db.getCollection("users");
|
|
244
|
-
const user = await User.model.findOne({
|
|
245
|
-
where: {
|
|
246
|
-
phone: values.phone
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
if (!user) {
|
|
250
|
-
return ctx.throw(404, ctx.t("The phone number is incorrect, please re-enter", { ns: import__.namespace }));
|
|
251
|
-
}
|
|
252
|
-
ctx.state.currentUser = user;
|
|
253
|
-
return next();
|
|
254
|
-
})
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
176
|
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"displayName.zh-CN": "用户",
|
|
5
5
|
"description": "Provides a basic user model and a password-based user authentication type, and extended the createdBy and updatedBy field types",
|
|
6
6
|
"description.zh-CN": "提供了基础的用户模型和基于密码的用户认证方式,并拓展了创建人和最后更新人字段类型",
|
|
7
|
-
"version": "0.
|
|
7
|
+
"version": "0.17.0-alpha.1",
|
|
8
8
|
"license": "AGPL-3.0",
|
|
9
9
|
"main": "./dist/server/index.js",
|
|
10
10
|
"devDependencies": {
|
|
@@ -22,5 +22,5 @@
|
|
|
22
22
|
"@nocobase/test": "0.x",
|
|
23
23
|
"@nocobase/utils": "0.x"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "afbb7e79fe2633292bc161d56d23c684181632ed"
|
|
26
26
|
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var authenticators_exports = {};
|
|
29
|
-
__export(authenticators_exports, {
|
|
30
|
-
default: () => authenticators_default
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(authenticators_exports);
|
|
33
|
-
var import_path = __toESM(require("path"));
|
|
34
|
-
var import_utils = require("@nocobase/utils");
|
|
35
|
-
function authenticators_default(plugin, more = {}) {
|
|
36
|
-
const { authenticators } = plugin;
|
|
37
|
-
const natives = ["password"].reduce(
|
|
38
|
-
(result, key) => Object.assign(result, {
|
|
39
|
-
[key]: (0, import_utils.requireModule)(import_path.default.isAbsolute(key) ? key : import_path.default.join(__dirname, key))
|
|
40
|
-
}),
|
|
41
|
-
{}
|
|
42
|
-
);
|
|
43
|
-
for (const [name, authenticator] of Object.entries({ ...more, ...natives })) {
|
|
44
|
-
authenticators.register(name, authenticator);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var password_exports = {};
|
|
19
|
-
__export(password_exports, {
|
|
20
|
-
default: () => password_default
|
|
21
|
-
});
|
|
22
|
-
module.exports = __toCommonJS(password_exports);
|
|
23
|
-
var import__ = require("..");
|
|
24
|
-
async function password_default(ctx, next) {
|
|
25
|
-
const { uniqueField = "email", values } = ctx.action.params;
|
|
26
|
-
if (!values[uniqueField]) {
|
|
27
|
-
return ctx.throw(400, {
|
|
28
|
-
code: "InvalidUserData",
|
|
29
|
-
message: ctx.t("Please fill in your email address", { ns: import__.namespace })
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
const User = ctx.db.getCollection("users");
|
|
33
|
-
const user = await User.model.findOne({
|
|
34
|
-
where: {
|
|
35
|
-
[uniqueField]: values[uniqueField]
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
if (!user) {
|
|
39
|
-
return ctx.throw(404, ctx.t("The email is incorrect, please re-enter", { ns: import__.namespace }));
|
|
40
|
-
}
|
|
41
|
-
const field = User.getField("password");
|
|
42
|
-
const valid = await field.verify(values.password, user.password);
|
|
43
|
-
if (!valid) {
|
|
44
|
-
return ctx.throw(404, ctx.t("The password is incorrect, please re-enter", { ns: import__.namespace }));
|
|
45
|
-
}
|
|
46
|
-
ctx.state.currentUser = user;
|
|
47
|
-
return next();
|
|
48
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export interface JwtOptions {
|
|
2
|
-
secret: string;
|
|
3
|
-
expiresIn?: string;
|
|
4
|
-
}
|
|
5
|
-
export declare class JwtService {
|
|
6
|
-
protected options: JwtOptions;
|
|
7
|
-
constructor(options: JwtOptions);
|
|
8
|
-
private expiresIn;
|
|
9
|
-
private secret;
|
|
10
|
-
sign(payload: any): string;
|
|
11
|
-
decode(token: string): Promise<any>;
|
|
12
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var jwt_service_exports = {};
|
|
29
|
-
__export(jwt_service_exports, {
|
|
30
|
-
JwtService: () => JwtService
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(jwt_service_exports);
|
|
33
|
-
var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
|
|
34
|
-
class JwtService {
|
|
35
|
-
constructor(options) {
|
|
36
|
-
this.options = options;
|
|
37
|
-
}
|
|
38
|
-
expiresIn() {
|
|
39
|
-
return this.options.expiresIn || process.env.JWT_EXPIRES_IN || "7d";
|
|
40
|
-
}
|
|
41
|
-
secret() {
|
|
42
|
-
return this.options.secret || process.env.APP_KEY;
|
|
43
|
-
}
|
|
44
|
-
sign(payload) {
|
|
45
|
-
return import_jsonwebtoken.default.sign(payload, this.secret(), { expiresIn: this.expiresIn() });
|
|
46
|
-
}
|
|
47
|
-
decode(token) {
|
|
48
|
-
return new Promise((resolve, reject) => {
|
|
49
|
-
import_jsonwebtoken.default.verify(token, this.secret(), (err, decoded) => {
|
|
50
|
-
if (err) {
|
|
51
|
-
return reject(err);
|
|
52
|
-
}
|
|
53
|
-
resolve(decoded);
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
59
|
-
0 && (module.exports = {
|
|
60
|
-
JwtService
|
|
61
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function check(options: any): (ctx: any, next: any) => Promise<any>;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var check_exports = {};
|
|
19
|
-
__export(check_exports, {
|
|
20
|
-
check: () => check
|
|
21
|
-
});
|
|
22
|
-
module.exports = __toCommonJS(check_exports);
|
|
23
|
-
function check(options) {
|
|
24
|
-
return async function check2(ctx, next) {
|
|
25
|
-
const { currentUser } = ctx.state;
|
|
26
|
-
if (!currentUser) {
|
|
27
|
-
return ctx.throw(401, "Unauthorized");
|
|
28
|
-
}
|
|
29
|
-
return next();
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
33
|
-
0 && (module.exports = {
|
|
34
|
-
check
|
|
35
|
-
});
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var middlewares_exports = {};
|
|
19
|
-
__export(middlewares_exports, {
|
|
20
|
-
check: () => import_check.check,
|
|
21
|
-
parseToken: () => import_parseToken.parseToken
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(middlewares_exports);
|
|
24
|
-
var import_check = require("./check");
|
|
25
|
-
var import_parseToken = require("./parseToken");
|
|
26
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
27
|
-
0 && (module.exports = {
|
|
28
|
-
check,
|
|
29
|
-
parseToken
|
|
30
|
-
});
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var parseToken_exports = {};
|
|
19
|
-
__export(parseToken_exports, {
|
|
20
|
-
parseToken: () => parseToken
|
|
21
|
-
});
|
|
22
|
-
module.exports = __toCommonJS(parseToken_exports);
|
|
23
|
-
async function parseToken(ctx, next) {
|
|
24
|
-
const user = await findUserByToken(ctx);
|
|
25
|
-
if (user) {
|
|
26
|
-
ctx.state.currentUser = user;
|
|
27
|
-
}
|
|
28
|
-
return next();
|
|
29
|
-
}
|
|
30
|
-
async function findUserByToken(ctx) {
|
|
31
|
-
const token = ctx.getBearerToken();
|
|
32
|
-
if (!token) {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
const { jwtService } = ctx.app.getPlugin("users");
|
|
36
|
-
try {
|
|
37
|
-
const { userId } = await jwtService.decode(token);
|
|
38
|
-
const collection = ctx.db.getCollection("users");
|
|
39
|
-
ctx.state.currentUserAppends = ctx.state.currentUserAppends || [];
|
|
40
|
-
for (const [, field] of collection.fields) {
|
|
41
|
-
if (field.type === "belongsTo") {
|
|
42
|
-
ctx.state.currentUserAppends.push(field.name);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const user = await ctx.db.getRepository("users").findOne({
|
|
46
|
-
appends: ctx.state.currentUserAppends,
|
|
47
|
-
filter: {
|
|
48
|
-
id: userId
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
ctx.logger.info(`Current user id: ${userId}`);
|
|
52
|
-
return user;
|
|
53
|
-
} catch (error) {
|
|
54
|
-
console.log(error);
|
|
55
|
-
ctx.logger.error(error);
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
60
|
-
0 && (module.exports = {
|
|
61
|
-
parseToken
|
|
62
|
-
});
|