@cloudbase/auth 2.23.4-alpha.0 → 2.24.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/src/type.d.ts ADDED
@@ -0,0 +1,303 @@
1
+ import type { authModels, AuthError, OAUTH_TYPE } from '@cloudbase/oauth'
2
+
3
+ export interface SignInAnonymouslyReq {
4
+ provider_token?: string // 提供令牌
5
+ }
6
+
7
+ export declare type User = {
8
+ id: any // 用户ID
9
+ aud: string // 受众
10
+ role: string[] // 用户角色
11
+ email: any // 邮箱
12
+ email_confirmed_at: string // 邮箱确认时间
13
+ phone: any // 手机号
14
+ phone_confirmed_at: string // 手机号确认时间
15
+ confirmed_at: string // 确认时间
16
+ last_sign_in_at: string // 最后登录时间
17
+ app_metadata: {
18
+ // 应用元数据
19
+ provider: any // 提供商
20
+ providers: any[] // 提供商列表
21
+ }
22
+ user_metadata: {
23
+ // 用户元数据
24
+ name: any // 姓名
25
+ picture: any // 头像
26
+ username: any // 用户名
27
+ gender: any // 性别
28
+ locale: any // 地区
29
+ uid: any // 用户ID
30
+ nickName: any // 昵称
31
+ avatarUrl: any // 头像URL
32
+ location: any // 位置
33
+ hasPassword: any // 是否有密码
34
+ }
35
+ identities: any // 身份信息
36
+ created_at: string // 创建时间
37
+ updated_at: string // 更新时间
38
+ is_anonymous: boolean // 是否匿名用户
39
+ }
40
+
41
+ export declare type Session = {
42
+ access_token: string // 访问令牌
43
+ refresh_token: string // 刷新令牌
44
+ expires_in: number // 过期时间(秒)
45
+ token_type: string // 令牌类型
46
+ user: User // 用户信息
47
+ }
48
+
49
+ export interface SignInWithPasswordReq {
50
+ username?: string // 用户名称(可选,与邮箱、手机号三选一),长度 5-24 位,支持字符中英文、数字、特殊字符(仅支持_-),不支持中文
51
+ email?: string // 邮箱(可选,与用户名、手机号三选一)
52
+ phone?: string // 手机号(可选,与用户名、邮箱三选一)
53
+ password: string // 密码(必填)
54
+ }
55
+
56
+ // 通用响应类型
57
+ export interface CommonRes {
58
+ data: {} // 成功时为空对象
59
+ error: AuthError | null // 错误信息,成功时为null
60
+ }
61
+
62
+ export interface SignInRes {
63
+ data: {
64
+ user?: User // 用户信息
65
+ session?: Session // 会话信息
66
+ }
67
+ error: AuthError | null // 错误信息,成功时为null
68
+ }
69
+
70
+ export interface GetUserRes {
71
+ data: {
72
+ user?: User // 用户详细信息
73
+ }
74
+ error: AuthError | null // 错误信息,成功时为null
75
+ }
76
+
77
+ // 更新用户验证回调参数
78
+ export interface UpdateUserVerifyOtpParams {
79
+ email?: string // 邮箱(可选,与手机号二选一)
80
+ phone?: string // 手机号(可选,与邮箱二选一)
81
+ token: string // 验证码(必填)
82
+ }
83
+
84
+ // 更新用户验证回调类型
85
+ declare type UpdateUserVerifyCallback = (params: UpdateUserVerifyOtpParams) => Promise<GetUserRes>
86
+
87
+ // 更新用户响应类型(当需要验证时)
88
+ export interface UpdateUserWithVerificationRes {
89
+ data: {
90
+ verifyOtp?: UpdateUserVerifyCallback // 验证码回调函数
91
+ messageId?: string // 验证码ID
92
+ }
93
+ error: AuthError | null // 错误信息,成功时为null
94
+ }
95
+
96
+ export interface SignInWithIdTokenReq {
97
+ provider?: string // 第三方平台标识(可选)
98
+ token: string // 第三方平台的身份令牌(必填)
99
+ }
100
+
101
+ export interface SignInWithOAuthReq {
102
+ provider: string // 第三方平台标识(必填)
103
+ options?: {
104
+ // 配置选项(可选)
105
+ redirectTo?: string // 回调地址,默认为当前页面
106
+ skipBrowserRedirect?: boolean // 是否跳转至授权页面,默认为false
107
+ state?: string // 状态参数,用于安全验证,默认为随机字符串(格式:prd-{provider}-{随机字符串})
108
+ queryParams?: Record<string, string> // 额外的查询参数,将合并到授权 URI 中
109
+ type?: (typeof OAUTH_TYPE)[keyof typeof OAUTH_TYPE] // 类型(可选),默认为'sign_in', sign_in: 登录,bind_identity: 绑定身份
110
+ }
111
+ }
112
+
113
+ export interface SignInOAuthRes {
114
+ data: {
115
+ url?: string // 授权页面URL
116
+ provider?: string // 第三方平台标识
117
+ scopes?: string // 授权范围
118
+ }
119
+ error: AuthError | null // 错误信息,成功时为null
120
+ }
121
+
122
+ export interface GetClaimsRes {
123
+ data: {
124
+ // 令牌声明信息
125
+ claims?: {
126
+ iss: string // issuer
127
+ sub: string // subject
128
+ aud: string // audience
129
+ exp: number // expiration time
130
+ iat: number // issued at
131
+ at_hash: string // access token hash
132
+ name: string // 名称
133
+ picture?: string // 头像URL
134
+ email?: string // 邮箱
135
+ phone_number?: string // 手机号
136
+ scope: string // 授权范围
137
+ project_id: string // 项目ID
138
+ provider?: string // 第三方平台标识
139
+ provider_type?: string // 第三方平台类型
140
+ groups?: string[] // 用户组
141
+ meta?: {
142
+ wxOpenId?: string
143
+ wxUnionId?: string
144
+ }
145
+ user_id: string // 用户ID
146
+ roles: string[] // 角色
147
+ user_type: string // 用户类型
148
+ client_type: string // 客户端类型
149
+ is_system_admin: boolean // 是否系统管理员
150
+ }
151
+ // 令牌头部信息
152
+ header?: {
153
+ alg: string // 加密算法
154
+ kid: string // 令牌ID
155
+ }
156
+ signature?: string // 令牌签名
157
+ }
158
+ error: AuthError | null // 错误信息,成功时为null
159
+ }
160
+
161
+ export interface UpdateUserReq extends authModels.ModifyUserBasicInfoRequest {
162
+ email?: string // 邮箱(可选)
163
+ phone?: string // 手机号(可选)
164
+ username?: string // 用户名称(可选),长度 5-24 位,支持字符中英文、数字、特殊字符(仅支持_-),不支持中文
165
+ description?: string // 描述(可选)
166
+ avatar_url?: string // 头像URL(可选)
167
+ nickname?: string // 昵称(可选)
168
+ gender?: 'MALE' | 'FEMALE' // 性别(可选)
169
+ }
170
+
171
+ export interface GetUserIdentitiesRes {
172
+ data: {
173
+ identities?:
174
+ | Array<{
175
+ id: string // 身份源标识
176
+ name: string // 身份源名称
177
+ picture: string // 头像URL
178
+ }>
179
+ | authModels.ProvidersResponse['data']
180
+ }
181
+ error: AuthError | null // 错误信息,成功时为null
182
+ }
183
+
184
+ export interface LinkIdentityReq {
185
+ provider: string // 身份源标识(必填)
186
+ }
187
+
188
+ export interface UnlinkIdentityReq {
189
+ provider: string // 身份源标识(必填)
190
+ }
191
+
192
+ export interface LinkIdentityRes {
193
+ data: {
194
+ provider?: string // 绑定的身份源标识
195
+ type?: (typeof OAUTH_TYPE)[keyof typeof OAUTH_TYPE] // 类型(可选),默认为'sign_in', sign_in: 登录,bind_identity: 绑定身份
196
+ }
197
+ error: AuthError | null // 错误信息,成功时为null
198
+ }
199
+
200
+ export declare type OnAuthStateChangeEvent =
201
+ | 'SIGNED_OUT' // 用户已登出
202
+ | 'SIGNED_IN' // 用户登录成功
203
+ | 'INITIAL_SESSION' // 初始会话已建立
204
+ | 'PASSWORD_RECOVERY' // 密码已重置
205
+ | 'TOKEN_REFRESHED' // 令牌已刷新
206
+ | 'USER_UPDATED' // 用户信息已更新
207
+ | 'BIND_IDENTITY' // 身份源绑定结果
208
+
209
+ export declare type OnAuthStateChangeCallback = (
210
+ event: OnAuthStateChangeEvent,
211
+ session: Session,
212
+ info?: Record<string, any>,
213
+ ) => void
214
+
215
+ type MobileOtpType = 'sms' | 'phone_change'
216
+ type EmailOtpType = 'signup' | 'invite' | 'magiclink' | 'recovery' | 'email_change' | 'email'
217
+ export interface VerifyOtpReq {
218
+ type?: MobileOtpType | EmailOtpType
219
+ email?: string // 邮箱(可选,与手机号二选一)
220
+ phone?: string // 手机号(可选,与邮箱二选一)
221
+ token: string // 验证码(必填)
222
+ messageId: string // 验证码对应ID(必填)
223
+ }
224
+
225
+ export interface SignInWithOtpReq {
226
+ email?: string // 邮箱(可选,与手机号二选一)
227
+ phone?: string // 手机号(可选,与邮箱二选一)
228
+ }
229
+ /**
230
+ *
231
+ * @param code 验证码
232
+ * @param messageId 可选,ResendRes.data.messageId
233
+ * @returns
234
+ */
235
+ declare type OtpCallback = (params: VerifyOtpReq) => Promise<verifyOtpRes>
236
+
237
+ export interface SignInWithOtpRes {
238
+ data: SignInRes['data'] & {
239
+ verifyOtp?: OtpCallback // 验证码回调函数,支持messageId参数
240
+ }
241
+ error: AuthError | null // 错误信息,成功时为null
242
+ }
243
+
244
+ export interface SignUpRes {
245
+ data: {
246
+ verifyOtp?: OtpCallback // 验证码回调函数,支持messageId参数
247
+ }
248
+ error: AuthError | null
249
+ }
250
+
251
+ export interface ResendReq {
252
+ email?: string // 邮箱(可选,与手机号二选一)
253
+ phone?: string // 手机号(可选,与邮箱二选一)
254
+ type?: 'signup' | 'email_change' | 'phone_change' | 'sms' // 类型(可选)
255
+ }
256
+
257
+ export interface ResendRes {
258
+ data: {
259
+ messageId?: string // 消息ID(验证码ID)
260
+ }
261
+ error: AuthError | null // 错误信息,成功时为null
262
+ }
263
+
264
+ export interface VerifyOAuthReq {
265
+ code?: string // 授权码(可选,默认从URL参数获取)
266
+ state?: string // 状态参数(可选,默认从URL参数获取)
267
+ provider?: string // 第三方平台标识(可选,默认从session获取)
268
+ }
269
+
270
+ export interface UpdateUserAttributes {
271
+ nonce: string // 验证码
272
+ password: string // 新密码
273
+ }
274
+
275
+ // 重置密码响应类型
276
+ export interface ResetPasswordForEmailRes {
277
+ data: {
278
+ updateUser?: (attributes: UpdateUserAttributes) => Promise<SignInRes> // 验证码回调函数,支持新密码参数
279
+ }
280
+ error: AuthError | null // 错误信息,成功时为null
281
+ }
282
+
283
+ // 重新认证响应类型
284
+ export interface ReauthenticateRes {
285
+ data: {
286
+ updateUser?: (attributes: UpdateUserAttributes) => Promise<SignInRes> // 验证码回调函数,支持新密码参数
287
+ }
288
+ error: AuthError | null // 错误信息,成功时为null
289
+ }
290
+
291
+ export interface SetSessionReq {
292
+ access_token: string // 访问令牌
293
+ refresh_token: string // 刷新令牌
294
+ }
295
+
296
+ export interface DeleteMeReq {
297
+ password: string // 用户密码
298
+ }
299
+
300
+ export interface ResetPasswordForOldReq {
301
+ old_password: string // 旧密码
302
+ new_password: string // 新密码
303
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,69 @@
1
+ /**
2
+ * 存储参数到浏览器sessionStorage
3
+ * @param key
4
+ * @param value
5
+ */
6
+ export function saveToBrowserSession(key: string, value: Record<string, any>) {
7
+ try {
8
+ sessionStorage.setItem(key, JSON.stringify(value))
9
+ } catch (error) {
10
+ console.error('Failed to save to browser session:', error)
11
+ }
12
+ }
13
+
14
+ export function getBrowserSession(key: string) {
15
+ try {
16
+ const data = JSON.parse(sessionStorage.getItem(key))
17
+ return data
18
+ } catch (error) {
19
+ console.error('Failed to get browser session:', error)
20
+ return null
21
+ }
22
+ }
23
+
24
+ export function removeBrowserSession(key: string) {
25
+ try {
26
+ sessionStorage.removeItem(key)
27
+ return true
28
+ } catch (error) {
29
+ console.error('Failed to remove browser session:', error)
30
+ return null
31
+ }
32
+ }
33
+
34
+ export function addUrlSearch(search: Location['search'], hash?: Location['hash']) {
35
+ if (search === undefined) return
36
+
37
+ // 1. 解析当前 URL
38
+ const url = new URL(window.location.href)
39
+
40
+ // 2. 操作查询参数
41
+ url.search = search // 覆盖已有参数或新增参数
42
+
43
+ url.hash = hash || url.hash
44
+
45
+ // 3. 更新地址栏(保留哈希值)
46
+ window.history.replaceState({}, '', url.href)
47
+ }
48
+
49
+ /**
50
+ * 从 URL 中移除指定的查询参数
51
+ * @param params 要移除的参数名数组
52
+ */
53
+ export function removeUrlParams(params: string[]) {
54
+ if (typeof window === 'undefined') return
55
+
56
+ try {
57
+ const url = new URL(window.location.href)
58
+
59
+ // 移除指定的参数
60
+ params.forEach((param) => {
61
+ url.searchParams.delete(param)
62
+ })
63
+
64
+ // 使用 replaceState 更新 URL,不会在浏览器历史中留下记录
65
+ window.history.replaceState({}, '', url.href)
66
+ } catch (error) {
67
+ console.error('Failed to remove URL params:', error)
68
+ }
69
+ }