@tachybase/plugin-auth-main-app 1.3.23 → 1.4.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.
@@ -1,6 +1,6 @@
1
1
  module.exports = {
2
- "@tachybase/client": "1.3.25",
3
- "@tachybase/module-auth": "1.3.25",
2
+ "@tachybase/client": "1.4.0",
3
+ "@tachybase/module-auth": "1.4.0",
4
4
  "react": "18.3.1",
5
5
  "@tachybase/schema": "1.3.52",
6
6
  "antd": "5.22.5",
@@ -3,11 +3,14 @@
3
3
  "Auth by main app": "通过主应用登录",
4
4
  "Can not manage this sub app!": "没有权限管理此应用",
5
5
  "Enable Single Sign-On (SSO)": "允许统一登录(SSO)",
6
+ "Failed to create or find current user": "创建或查找当前用户失败",
6
7
  "Failed to fetch authentication information. Please try again.": "获取认证信息失败。请再试一次。",
8
+ "Invalid token or user not found": "无效的令牌或用户未找到",
7
9
  "It is impossible to delete all login verification methods.": "无法删除所有登录验证方式",
8
10
  "Main app signIn": "主应用登录",
9
11
  "Please log in to the main application first": "请先登录主应用",
10
12
  "Single Sign-On (SSO) between applications": "跨应用的单点登录(SSO)",
13
+ "The user only has a username. Please enter the application to complete the password and other information.": "该用户只有用户名,请进入应用完善密码和其他信息",
11
14
  "Unable to disable all authenticators in the main application.": "无法在主应用禁用所有认证器",
12
15
  "Unable to manage this application, please exit the main application and change to a new account": "无法管理此应用,请在主应用退出换一个账号"
13
16
  }
@@ -87,6 +87,9 @@ class AuthMainAppController {
87
87
  } catch (err) {
88
88
  ctx.throw(401, ctx.t("Please log in to the main application first", { ns: import_constants.NAMESPACE }));
89
89
  }
90
+ if (!user || !user.userId) {
91
+ ctx.throw(401, ctx.t("Invalid token or user not found", { ns: import_constants.NAMESPACE }));
92
+ }
90
93
  const multiAppRepo = mainApp.db.getRepository("applications");
91
94
  const multiApp = await multiAppRepo.findOne({
92
95
  filter: {
@@ -101,17 +104,43 @@ class AuthMainAppController {
101
104
  })
102
105
  );
103
106
  }
107
+ const mainUserRepo = mainApp.db.getRepository("users");
108
+ const userInfo = await mainUserRepo.findOne({
109
+ fields: ["username", "nickname", "phone"],
110
+ filter: {
111
+ id: user.userId
112
+ },
113
+ raw: true
114
+ });
104
115
  const repo = ctx.db.getRepository("users");
105
- const root = await repo.findOne({
116
+ let currentUser;
117
+ if (!userInfo) {
118
+ ctx.throw(403, ctx.t("User info not found in main application", { ns: import_constants.NAMESPACE }));
119
+ }
120
+ currentUser = await repo.findOne({
106
121
  filter: {
107
- specialRole: "root"
122
+ $or: [
123
+ ...userInfo.username ? [{ username: userInfo.username }] : [],
124
+ ...userInfo.phone ? [{ phone: userInfo.phone }] : []
125
+ ]
108
126
  }
109
127
  });
110
- const tokenInfo = await mainApp.authManager.tokenController.add({ userId: root.id });
128
+ if (!currentUser) {
129
+ const newUserData = {};
130
+ if (userInfo.username) newUserData.username = userInfo.username;
131
+ if (userInfo.nickname) newUserData.nickname = userInfo.nickname;
132
+ if (userInfo.phone) newUserData.phone = userInfo.phone;
133
+ currentUser = await repo.create({ values: newUserData });
134
+ }
135
+ if (!currentUser) {
136
+ ctx.throw(500, ctx.t("Failed to create or find current user", { ns: import_constants.NAMESPACE }));
137
+ }
138
+ const currentUserData = currentUser == null ? void 0 : currentUser.dataValues;
139
+ const tokenInfo = await mainApp.authManager.tokenController.add({ userId: currentUserData.id });
111
140
  const expiresIn = Math.floor((await mainApp.authManager.tokenController.getConfig()).tokenExpirationTime / 1e3);
112
141
  const newToken = ctx.app.authManager.jwt.sign(
113
142
  {
114
- userId: root.id,
143
+ userId: currentUserData.id,
115
144
  temp: true,
116
145
  iat: Math.floor(tokenInfo.issuedTime / 1e3),
117
146
  signInTime: tokenInfo.signInTime
@@ -121,18 +150,15 @@ class AuthMainAppController {
121
150
  expiresIn
122
151
  }
123
152
  );
124
- const mainUserRepo = mainApp.db.getRepository("users");
125
- const userInfo = await mainUserRepo.findOne({
126
- fields: ["username", "nickname", "phone"],
127
- filter: {
128
- id: user.userId
129
- },
130
- raw: true
131
- });
132
153
  ctx.body = {
133
154
  ...userInfo,
134
155
  token: newToken
135
156
  };
157
+ if (userInfo.username && !userInfo.phone) {
158
+ console.log(
159
+ "The user only has a username. Please enter the application to complete the password and other information."
160
+ );
161
+ }
136
162
  return next();
137
163
  }
138
164
  async get(ctx, next) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tachybase/plugin-auth-main-app",
3
3
  "displayName": "Single Sign-On on multi app (SSO)",
4
- "version": "1.3.23",
4
+ "version": "1.4.0",
5
5
  "description": "Users log in via the main application; sub-applications can disable their standalone login.",
6
6
  "keywords": [
7
7
  "Authentication"
@@ -13,8 +13,8 @@
13
13
  "@tego/client": "*",
14
14
  "@tego/server": "*",
15
15
  "antd": "5.22.5",
16
- "@tachybase/client": "1.3.25",
17
- "@tachybase/module-auth": "1.3.25"
16
+ "@tachybase/client": "1.4.0",
17
+ "@tachybase/module-auth": "1.4.0"
18
18
  },
19
19
  "description.zh-CN": "通过主应用登录子应用,子应用可关闭登录",
20
20
  "displayName.zh-CN": "多应用统一登录"