weifuwu 0.80.0 → 0.81.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/dist/components/index.js +12 -12
- package/dist/index.js +219 -8
- package/dist/ui-dom/Confirm.d.ts +1 -1
- package/dist/ui-dom/Notification.d.ts +1 -1
- package/dist/ui-dom/Toast.d.ts +1 -1
- package/dist/ui-dom/hooks/chat.d.ts +1 -1
- package/dist/ui-dom/hooks/external.d.ts +1 -1
- package/dist/ui-dom/index.d.ts +1 -1
- package/dist/ui-dom/index.js +9 -8
- package/dist/ui-dom/middleware/api.d.ts +3 -0
- package/dist/ui-dom/middleware/auth.d.ts +2 -0
- package/dist/ui-dom/store.d.ts +1 -1
- package/dist/ui-dom/types.d.ts +1 -1
- package/dist/ui-dom/use-chat.d.ts +1 -1
- package/dist/ui-dom/vdom2/audit.d.ts +1 -1
- package/dist/ui-dom/vdom2/mount.d.ts +1 -1
- package/dist/ui-dom/vdom2/trace.d.ts +1 -1
- package/dist/ui-dom/vdom2/transform.d.ts +1 -1
- package/dist/user/index.d.ts +85 -7
- package/package.json +1 -1
package/dist/user/index.d.ts
CHANGED
|
@@ -1,19 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* weifuwu/userSystem —
|
|
2
|
+
* weifuwu/userSystem — 用户系统中间件(平台 → 应用 → 应用用户 三层模型)
|
|
3
3
|
*
|
|
4
4
|
* 注入 ctx.user(从 Authorization: Bearer 解码 + 查库)+ ctx.auth(方法面)。
|
|
5
5
|
* users.routes(app) 注册 /api/auth/* 路由;users.migrate() 幂等建表。
|
|
6
6
|
*
|
|
7
|
+
* 三层模型(用户决策 2026-12):
|
|
8
|
+
* 1. 平台注册(email 全局唯一)→ 平台账号(_weifuwu_users,无租户绑定)
|
|
9
|
+
* 2. createApp 建应用 → 调用者成为 owner(_weifuwu_apps + _weifuwu_app_members)
|
|
10
|
+
* 3. 应用内注册:自助(open_registration=true)或邀请(invite token)双模式
|
|
11
|
+
* 登录入口:平台登录(/api/auth/login,token 无 appId + 我的应用列表)、
|
|
12
|
+
* 应用内登录(/api/auth/apps/:appSlug/login,token 带 appId + role)
|
|
13
|
+
* 鉴权:requireApp(appId) 应用级成员校验;token payload appId → ctx.appId 注入
|
|
14
|
+
*
|
|
7
15
|
* 安全基线:
|
|
8
16
|
* - scrypt 密码哈希(per-user salt + timing-safe compare,异步不阻塞)
|
|
9
17
|
* - access token:HMAC-SHA256 JWT(无状态,与 client auth() 兼容)
|
|
10
18
|
* - refresh token:不透明随机串,DB 只存哈希,logout/轮换即撤销
|
|
11
19
|
* - 登录失败统一 401(不泄露"邮箱是否存在"——防枚举)
|
|
20
|
+
* - 邀请 token:JWT 短效(7 天)+ email 绑定——非 owner 无法生成/消费
|
|
12
21
|
*
|
|
13
22
|
* 诚实裁剪:
|
|
14
|
-
* ✅ register/login/logout/me/refresh / setPassword / createToken / tenant claim
|
|
23
|
+
* ✅ register/login/logout/me/refresh / setPassword / createToken / tenant claim /
|
|
24
|
+
* apps 三层(createApp / registerInApp / loginApp / createInvite / addMember /
|
|
25
|
+
* listMyApps / requireApp)
|
|
15
26
|
* ❌ OAuth、邮箱验证邮件(给 createToken + setPassword 底层 API 自接)、
|
|
16
|
-
* 多因素、RBAC 权限引擎(只提供 role
|
|
27
|
+
* 多因素、RBAC 权限引擎(只提供 role 字段:owner/admin/member)、
|
|
28
|
+
* 订阅/计费/配额、应用业务数据(业务表挂 app_id 外键)、
|
|
29
|
+
* 应用删除的级联业务清理(只提供 deleteApp 删 app+members,业务表由业务层处理)
|
|
17
30
|
*
|
|
18
31
|
* ```ts
|
|
19
32
|
* const db = postgres()
|
|
@@ -25,8 +38,9 @@
|
|
|
25
38
|
* users.routes(app)
|
|
26
39
|
*
|
|
27
40
|
* app.get('/me', (req, ctx) => ok(ctx.user)) // ctx.user 已注入
|
|
28
|
-
* app.
|
|
41
|
+
* app.get('/secure', (req, ctx) => {
|
|
29
42
|
* ctx.auth.requireAuth() // 未登录抛 401
|
|
43
|
+
* ctx.auth.requireApp(ctx.appId) // 应用成员校验(async)
|
|
30
44
|
* })
|
|
31
45
|
* ```
|
|
32
46
|
*/
|
|
@@ -51,8 +65,40 @@ export interface RegisterInput {
|
|
|
51
65
|
password: string;
|
|
52
66
|
name?: string;
|
|
53
67
|
role?: string;
|
|
68
|
+
/** 兼容旧单层模型:注册即绑定应用(新三层模型请用 createApp/registerInApp) */
|
|
54
69
|
tenant?: string;
|
|
55
70
|
}
|
|
71
|
+
/** 应用(产品/tenant 隔离单元) */
|
|
72
|
+
export interface AppInfo {
|
|
73
|
+
id: string;
|
|
74
|
+
slug: string;
|
|
75
|
+
name: string;
|
|
76
|
+
owner_user_id: string;
|
|
77
|
+
open_registration: boolean;
|
|
78
|
+
created_at?: Date;
|
|
79
|
+
}
|
|
80
|
+
/** 我的应用摘要(平台登录/列表返回) */
|
|
81
|
+
export interface AppSummary {
|
|
82
|
+
id: string;
|
|
83
|
+
slug: string;
|
|
84
|
+
name: string;
|
|
85
|
+
role: string;
|
|
86
|
+
}
|
|
87
|
+
export interface CreateAppInput {
|
|
88
|
+
slug: string;
|
|
89
|
+
name: string;
|
|
90
|
+
/** 允许自助注册(默认 false——邀请制) */
|
|
91
|
+
openRegistration?: boolean;
|
|
92
|
+
}
|
|
93
|
+
export interface RegisterInAppInput {
|
|
94
|
+
/** 应用 slug(人类可读路由标识) */
|
|
95
|
+
appSlug: string;
|
|
96
|
+
email: string;
|
|
97
|
+
password: string;
|
|
98
|
+
name?: string;
|
|
99
|
+
/** 邀请制(open_registration=false)必传 */
|
|
100
|
+
inviteToken?: string;
|
|
101
|
+
}
|
|
56
102
|
/** ctx.auth 方法面 */
|
|
57
103
|
export interface AuthApi {
|
|
58
104
|
register(input: RegisterInput): Promise<{
|
|
@@ -64,6 +110,7 @@ export interface AuthApi {
|
|
|
64
110
|
token: string;
|
|
65
111
|
refreshToken: string;
|
|
66
112
|
user: User;
|
|
113
|
+
apps: AppSummary[];
|
|
67
114
|
}>;
|
|
68
115
|
/** 撤销 refresh token(logout) */
|
|
69
116
|
logout(refreshToken: string): Promise<void>;
|
|
@@ -75,6 +122,37 @@ export interface AuthApi {
|
|
|
75
122
|
createToken(type: string, payload: Record<string, unknown>, opts: {
|
|
76
123
|
ttlSeconds: number;
|
|
77
124
|
}): string;
|
|
125
|
+
/** 建应用 → 调用者成为 owner(需登录) */
|
|
126
|
+
createApp(input: CreateAppInput): Promise<AppInfo>;
|
|
127
|
+
/** 应用内注册:自助(open_registration=true)/ 邀请(inviteToken)——复用或创建平台账号 + 加成员 */
|
|
128
|
+
registerInApp(input: RegisterInAppInput): Promise<{
|
|
129
|
+
token: string;
|
|
130
|
+
refreshToken: string;
|
|
131
|
+
user: User;
|
|
132
|
+
}>;
|
|
133
|
+
/** 应用内登录:验证密码 + 应用成员资格 → token 带 appId + role */
|
|
134
|
+
loginApp(appSlug: string, email: string, password: string): Promise<{
|
|
135
|
+
token: string;
|
|
136
|
+
refreshToken: string;
|
|
137
|
+
user: User;
|
|
138
|
+
}>;
|
|
139
|
+
/** owner 生成邀请 token(7 天有效,可选绑定 email/role) */
|
|
140
|
+
createInvite(appId: string, opts: {
|
|
141
|
+
email?: string;
|
|
142
|
+
role?: string;
|
|
143
|
+
}): Promise<{
|
|
144
|
+
inviteToken: string;
|
|
145
|
+
}>;
|
|
146
|
+
/** owner 直接添加已有平台账号为成员 */
|
|
147
|
+
addMember(appId: string, email: string, role?: string): Promise<void>;
|
|
148
|
+
/** 我的应用列表(平台登录后选应用) */
|
|
149
|
+
listMyApps(): Promise<AppSummary[]>;
|
|
150
|
+
/** 应用级鉴权:未登录 401、非成员 403;返回成员信息 */
|
|
151
|
+
requireApp(appId: string): Promise<{
|
|
152
|
+
userId: string;
|
|
153
|
+
appId: string;
|
|
154
|
+
role: string;
|
|
155
|
+
}>;
|
|
78
156
|
}
|
|
79
157
|
export interface UserInjected {
|
|
80
158
|
/** 当前用户(未登录 = null)。每次请求从 token 解码 + 查库(删号/角色变更即时生效) */
|
|
@@ -87,12 +165,12 @@ declare module '../types.ts' {
|
|
|
87
165
|
}
|
|
88
166
|
}
|
|
89
167
|
export interface UserSystemClient extends Middleware<Context, Context & UserInjected> {
|
|
90
|
-
/** 幂等建表(users + sessions) */
|
|
168
|
+
/** 幂等建表(users + sessions + apps + app_members) */
|
|
91
169
|
migrate: () => Promise<void>;
|
|
92
|
-
/** 注册 /api/auth/*
|
|
170
|
+
/** 注册 /api/auth/* 路由;exclude 跳过个别路由(应用层自定义) */
|
|
93
171
|
routes: (app: Router<any>, opts?: {
|
|
94
172
|
prefix?: string;
|
|
95
|
-
exclude?: Array<'register' | 'login' | 'logout' | 'refresh' | 'me'>;
|
|
173
|
+
exclude?: Array<'register' | 'login' | 'logout' | 'refresh' | 'me' | 'apps'>;
|
|
96
174
|
}) => void;
|
|
97
175
|
}
|
|
98
176
|
export declare function userSystem(options: UserSystemOptions): UserSystemClient;
|