@amaster.ai/auth-client 1.0.0-beta.6 → 1.0.0-beta.73
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/README.md +252 -79
- package/dist/auth.d.cts +66 -4
- package/dist/auth.d.ts +66 -4
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -4
- package/dist/index.d.ts +32 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/oauth.d.cts +1 -1
- package/dist/oauth.d.ts +1 -1
- package/dist/permissions.d.cts +14 -22
- package/dist/permissions.d.ts +14 -22
- package/dist/sessions.d.cts +1 -1
- package/dist/sessions.d.ts +1 -1
- package/dist/{types-Bgi_Lwkp.d.cts → types-DGF9cpAg.d.cts} +65 -68
- package/dist/{types-Bgi_Lwkp.d.ts → types-DGF9cpAg.d.ts} +65 -68
- package/dist/user.d.cts +1 -1
- package/dist/user.d.ts +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,7 +8,8 @@ Authentication SDK for Amaster Platform - Complete client-side authentication so
|
|
|
8
8
|
- 🔄 **Automatic Token Refresh**: JWT token auto-refresh before expiration
|
|
9
9
|
- 📦 **Token Storage**: localStorage or sessionStorage with SSR support
|
|
10
10
|
- 🎯 **Permission System**: Role-based and permission-based access control
|
|
11
|
-
-
|
|
11
|
+
- 👤 **Anonymous Access**: Automatic support for anonymous users with configurable permissions
|
|
12
|
+
- 🔗 **OAuth Integration**: Google, GitHub, WeChat OAuth, WeChat Mini Program, and custom OAuth providers
|
|
12
13
|
- 📡 **Event System**: Listen to login, logout, and token events
|
|
13
14
|
- 💾 **Session Management**: View and revoke active sessions
|
|
14
15
|
- 🔒 **Type-Safe**: Full TypeScript support
|
|
@@ -58,29 +59,56 @@ if (result.data) {
|
|
|
58
59
|
const user = await authClient.getMe();
|
|
59
60
|
|
|
60
61
|
// Check permissions
|
|
61
|
-
if (authClient.hasPermission("user
|
|
62
|
+
if (authClient.hasPermission("user", "read")) {
|
|
62
63
|
// Show user list
|
|
63
64
|
}
|
|
64
65
|
```
|
|
65
66
|
|
|
66
67
|
## Configuration
|
|
67
68
|
|
|
69
|
+
### Zero Configuration (Recommended)
|
|
70
|
+
|
|
71
|
+
The SDK works out of the box with **zero configuration**:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const authClient = createAuthClient();
|
|
75
|
+
|
|
76
|
+
// Storage auto-detects environment:
|
|
77
|
+
// - Browser → localStorage
|
|
78
|
+
// - WeChat Mini Program → wx.setStorageSync
|
|
79
|
+
// - SSR/Node.js → no-op (no persistence)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Optional Configuration
|
|
83
|
+
|
|
68
84
|
```typescript
|
|
69
85
|
interface AuthClientOptions {
|
|
70
86
|
baseURL?: string; // API base URL, defaults to window.location.origin
|
|
71
|
-
|
|
87
|
+
headers?: Record<string, string>; // Custom headers for all requests
|
|
72
88
|
onTokenExpired?: () => void; // Token expiration callback
|
|
73
89
|
onUnauthorized?: () => void; // Unauthorized (401) callback
|
|
90
|
+
autoHandleOAuthCallback?: boolean; // Auto-handle OAuth callback hash on init, default true
|
|
91
|
+
autoRedirectAfterLogin?: boolean; // Auto-consume ?redirect=... after any successful login, default true
|
|
74
92
|
}
|
|
75
93
|
```
|
|
76
94
|
|
|
95
|
+
**Example:**
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
const authClient = createAuthClient({
|
|
99
|
+
baseURL: "https://api.example.com",
|
|
100
|
+
onTokenExpired: () => (window.location.href = "/login"),
|
|
101
|
+
onUnauthorized: () => alert("Session expired"),
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
77
105
|
**Built-in Features (Zero Config):**
|
|
78
106
|
|
|
79
|
-
- ✅
|
|
80
|
-
- ✅
|
|
81
|
-
- ✅
|
|
82
|
-
- ✅
|
|
83
|
-
- ✅
|
|
107
|
+
- ✅ Auto environment detection (browser, WeChat Mini Program, SSR)
|
|
108
|
+
- ✅ Auto token refresh (5 minutes before expiry)
|
|
109
|
+
- ✅ Auto permission sync
|
|
110
|
+
- ✅ Auto anonymous support
|
|
111
|
+
- ✅ Smart storage (localStorage, wx.storage, no-op for SSR)
|
|
84
112
|
|
|
85
113
|
## Authentication
|
|
86
114
|
|
|
@@ -154,15 +182,21 @@ const redirectUrl = sessionStorage.getItem("oauth_redirect") || "/";
|
|
|
154
182
|
window.location.href = redirectUrl;
|
|
155
183
|
```
|
|
156
184
|
|
|
185
|
+
If the callback page URL already carries `?redirect=...` such as
|
|
186
|
+
`/login?redirect=%2Fapi%2Foauth%2Fauthorize...`, the SDK will reuse that
|
|
187
|
+
redirect target automatically after a successful OAuth callback. Both
|
|
188
|
+
`#access_token=...` and `#accessToken=...` callback hash formats are supported.
|
|
189
|
+
|
|
190
|
+
By default, the same `?redirect=...` handling is also applied to password login,
|
|
191
|
+
verification-code login, registration auto-login, and mini-program login. Set
|
|
192
|
+
`autoRedirectAfterLogin: false` to disable this behavior and fully control
|
|
193
|
+
navigation in the application layer.
|
|
194
|
+
|
|
157
195
|
**Popup window:**
|
|
158
196
|
|
|
159
197
|
```typescript
|
|
160
198
|
// Main page
|
|
161
|
-
const popup = window.open(
|
|
162
|
-
"/api/auth/oauth/google",
|
|
163
|
-
"oauth-login",
|
|
164
|
-
"width=600,height=700"
|
|
165
|
-
);
|
|
199
|
+
const popup = window.open("/api/auth/oauth/google", "oauth-login", "width=600,height=700");
|
|
166
200
|
|
|
167
201
|
window.addEventListener("message", (event) => {
|
|
168
202
|
if (event.data.type === "oauth-success") {
|
|
@@ -178,6 +212,177 @@ if (window.opener) {
|
|
|
178
212
|
}
|
|
179
213
|
```
|
|
180
214
|
|
|
215
|
+
### WeChat Mini Program Login
|
|
216
|
+
|
|
217
|
+
The SDK automatically detects WeChat Mini Program environment and uses `wx.storage`:
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { createAuthClient } from "@amaster.ai/auth-client";
|
|
221
|
+
|
|
222
|
+
// Zero configuration - auto-detects WeChat environment
|
|
223
|
+
const authClient = createAuthClient({
|
|
224
|
+
baseURL: "https://api.yourdomain.com",
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// Login with WeChat code
|
|
228
|
+
wx.login({
|
|
229
|
+
success: async (res) => {
|
|
230
|
+
if (res.code) {
|
|
231
|
+
const result = await authClient.loginWithMiniProgram(res.code);
|
|
232
|
+
|
|
233
|
+
if (result.data) {
|
|
234
|
+
console.log("Logged in:", result.data.user);
|
|
235
|
+
// Token automatically saved to wx.storage
|
|
236
|
+
wx.switchTab({ url: "/pages/index/index" });
|
|
237
|
+
} else if (result.error) {
|
|
238
|
+
wx.showToast({
|
|
239
|
+
title: result.error.message || "Login failed",
|
|
240
|
+
icon: "none",
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
fail: (err) => {
|
|
246
|
+
console.error("wx.login failed:", err);
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
**Get user phone number (optional):**
|
|
252
|
+
|
|
253
|
+
```xml
|
|
254
|
+
<!-- WXML -->
|
|
255
|
+
<button
|
|
256
|
+
open-type="getPhoneNumber"
|
|
257
|
+
bindgetphonenumber="onGetPhoneNumber"
|
|
258
|
+
>
|
|
259
|
+
Get Phone Number
|
|
260
|
+
</button>
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
// JS
|
|
265
|
+
async onGetPhoneNumber(e) {
|
|
266
|
+
const { code } = e.detail;
|
|
267
|
+
|
|
268
|
+
if (code) {
|
|
269
|
+
const result = await authClient.getMiniProgramPhoneNumber(code);
|
|
270
|
+
|
|
271
|
+
if (result.data) {
|
|
272
|
+
console.log("Phone number:", result.data.phone);
|
|
273
|
+
console.log("Verified:", result.data.phoneVerified);
|
|
274
|
+
|
|
275
|
+
// Update UI with phone number
|
|
276
|
+
this.setData({
|
|
277
|
+
phone: result.data.phone
|
|
278
|
+
});
|
|
279
|
+
} else if (result.error) {
|
|
280
|
+
wx.showToast({
|
|
281
|
+
title: result.error.message || 'Failed to get phone number',
|
|
282
|
+
icon: 'none'
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
} else {
|
|
286
|
+
// User denied authorization
|
|
287
|
+
console.log("User cancelled phone number authorization");
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
**Complete Mini Program example:**
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
// pages/login/login.js
|
|
296
|
+
import { createAuthClient } from "@amaster.ai/auth-client";
|
|
297
|
+
|
|
298
|
+
const authClient = createAuthClient({
|
|
299
|
+
baseURL: "https://api.yourdomain.com",
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
Page({
|
|
303
|
+
data: {
|
|
304
|
+
userInfo: null,
|
|
305
|
+
hasPhone: false,
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
// Auto-login on page load
|
|
309
|
+
onLoad() {
|
|
310
|
+
this.handleLogin();
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
// WeChat Mini Program login
|
|
314
|
+
async handleLogin() {
|
|
315
|
+
wx.showLoading({ title: "Logging in..." });
|
|
316
|
+
|
|
317
|
+
wx.login({
|
|
318
|
+
success: async (res) => {
|
|
319
|
+
if (res.code) {
|
|
320
|
+
const result = await authClient.loginWithMiniProgram(res.code);
|
|
321
|
+
|
|
322
|
+
wx.hideLoading();
|
|
323
|
+
|
|
324
|
+
if (result.data) {
|
|
325
|
+
this.setData({
|
|
326
|
+
userInfo: result.data.user,
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
// Navigate to home
|
|
330
|
+
wx.switchTab({ url: "/pages/index/index" });
|
|
331
|
+
} else {
|
|
332
|
+
wx.showToast({
|
|
333
|
+
title: "Login failed",
|
|
334
|
+
icon: "none",
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
fail: () => {
|
|
340
|
+
wx.hideLoading();
|
|
341
|
+
wx.showToast({
|
|
342
|
+
title: "Login failed",
|
|
343
|
+
icon: "none",
|
|
344
|
+
});
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
},
|
|
348
|
+
|
|
349
|
+
// Get phone number with user authorization
|
|
350
|
+
async onGetPhoneNumber(e) {
|
|
351
|
+
const { code } = e.detail;
|
|
352
|
+
|
|
353
|
+
if (!code) {
|
|
354
|
+
wx.showToast({
|
|
355
|
+
title: "Authorization cancelled",
|
|
356
|
+
icon: "none",
|
|
357
|
+
});
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
wx.showLoading({ title: "Getting phone..." });
|
|
362
|
+
|
|
363
|
+
const result = await authClient.getMiniProgramPhoneNumber(code);
|
|
364
|
+
|
|
365
|
+
wx.hideLoading();
|
|
366
|
+
|
|
367
|
+
if (result.data) {
|
|
368
|
+
this.setData({
|
|
369
|
+
hasPhone: true,
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
wx.showToast({
|
|
373
|
+
title: "Phone number obtained",
|
|
374
|
+
icon: "success",
|
|
375
|
+
});
|
|
376
|
+
} else {
|
|
377
|
+
wx.showToast({
|
|
378
|
+
title: result.error?.message || "Failed to get phone",
|
|
379
|
+
icon: "none",
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
});
|
|
384
|
+
```
|
|
385
|
+
|
|
181
386
|
### Logout
|
|
182
387
|
|
|
183
388
|
```typescript
|
|
@@ -215,67 +420,50 @@ await authClient.changePassword({
|
|
|
215
420
|
|
|
216
421
|
## Permission Checks
|
|
217
422
|
|
|
218
|
-
###
|
|
423
|
+
### Anonymous Access Support
|
|
219
424
|
|
|
220
|
-
|
|
425
|
+
The SDK automatically supports anonymous users with **zero configuration**:
|
|
221
426
|
|
|
222
427
|
```typescript
|
|
223
|
-
//
|
|
224
|
-
|
|
225
|
-
// Admin only
|
|
226
|
-
}
|
|
428
|
+
// Create client
|
|
429
|
+
const authClient = createAuthClient();
|
|
227
430
|
|
|
228
|
-
//
|
|
229
|
-
if (authClient.hasPermission("
|
|
230
|
-
|
|
431
|
+
// Permission checks work for both authenticated and anonymous users
|
|
432
|
+
if (authClient.hasPermission("article", "read")) {
|
|
433
|
+
showArticleList();
|
|
231
434
|
}
|
|
232
435
|
|
|
233
|
-
// Check
|
|
234
|
-
if (authClient.
|
|
235
|
-
|
|
436
|
+
// Check if user is anonymous
|
|
437
|
+
if (authClient.isAnonymous()) {
|
|
438
|
+
showLoginPrompt();
|
|
236
439
|
}
|
|
237
440
|
|
|
238
|
-
//
|
|
239
|
-
|
|
240
|
-
// Has all permissions
|
|
241
|
-
}
|
|
441
|
+
// After login, permissions automatically update
|
|
442
|
+
await authClient.login({ ... });
|
|
242
443
|
```
|
|
243
444
|
|
|
244
|
-
###
|
|
445
|
+
### Local Permission Checks (Fast)
|
|
245
446
|
|
|
246
|
-
|
|
447
|
+
Permissions are cached locally for fast UI checks:
|
|
247
448
|
|
|
248
449
|
```typescript
|
|
249
|
-
//
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const { dataScope } = result.data;
|
|
253
|
-
|
|
254
|
-
if (dataScope.scopeType === "department") {
|
|
255
|
-
// Filter users by department
|
|
256
|
-
const users = await api.getUsers({
|
|
257
|
-
departmentId: dataScope.scopeFilter.departmentId
|
|
258
|
-
});
|
|
259
|
-
} else if (dataScope.scopeType === "all") {
|
|
260
|
-
// Show all users
|
|
261
|
-
const users = await api.getUsers();
|
|
262
|
-
}
|
|
450
|
+
// Check role
|
|
451
|
+
if (authClient.hasRole("admin")) {
|
|
452
|
+
showAdminPanel();
|
|
263
453
|
}
|
|
264
|
-
```
|
|
265
454
|
|
|
266
|
-
|
|
455
|
+
if (authClient.isAnonymous()) {
|
|
456
|
+
showLoginPrompt();
|
|
457
|
+
}
|
|
267
458
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
uid: "xxx",
|
|
272
|
-
email: "user@example.com",
|
|
273
|
-
roles: ["admin", "user"], // Only role codes
|
|
274
|
-
permissions: ["user.read", "user.write", "order.read"], // Only permission names
|
|
275
|
-
// NO dataScopes - loaded on demand
|
|
459
|
+
// Check permission
|
|
460
|
+
if (authClient.hasPermission("user", "read")) {
|
|
461
|
+
showUserList();
|
|
276
462
|
}
|
|
277
463
|
```
|
|
278
464
|
|
|
465
|
+
````
|
|
466
|
+
|
|
279
467
|
## OAuth Bindings
|
|
280
468
|
|
|
281
469
|
### Get Bindings
|
|
@@ -416,48 +604,32 @@ if (result.error) {
|
|
|
416
604
|
|
|
417
605
|
### Token Management
|
|
418
606
|
|
|
419
|
-
-
|
|
420
|
-
- Access Token
|
|
421
|
-
-
|
|
422
|
-
- Token is automatically refreshed 5 minutes before expiration (configurable)
|
|
423
|
-
|
|
424
|
-
### Permission Sync
|
|
425
|
-
|
|
426
|
-
- **On Page Load**: User info and permissions automatically sync from backend
|
|
427
|
-
- **On Token Refresh**: Permissions re-sync every 5 minutes when token refreshes
|
|
428
|
-
- **Manual Sync**: Call `await authClient.getMe()` to force sync anytime
|
|
429
|
-
|
|
430
|
-
**Behavior:**
|
|
431
|
-
- Permissions always stay fresh without any configuration
|
|
432
|
-
- Page refresh loads latest permissions from backend
|
|
433
|
-
- Background sync happens automatically every 5 minutes
|
|
607
|
+
- Tokens are automatically managed by the SDK
|
|
608
|
+
- Access Token refreshes 5 minutes before expiration
|
|
609
|
+
- No manual token handling required
|
|
434
610
|
|
|
435
611
|
### Permission Checks
|
|
436
612
|
|
|
437
|
-
-
|
|
438
|
-
-
|
|
439
|
-
-
|
|
613
|
+
- Use permission checks for UI control (show/hide buttons, menus)
|
|
614
|
+
- Frontend checks are NOT security measures
|
|
615
|
+
- Backend must always verify permissions
|
|
440
616
|
|
|
441
617
|
### SSR Support
|
|
442
618
|
|
|
443
|
-
The SDK
|
|
619
|
+
The SDK works in both browser and SSR environments:
|
|
444
620
|
|
|
445
621
|
```typescript
|
|
446
|
-
// Safe in both browser and SSR
|
|
447
622
|
const authClient = createAuthClient();
|
|
448
623
|
```
|
|
449
624
|
|
|
450
625
|
## TypeScript
|
|
451
626
|
|
|
452
|
-
Full TypeScript support
|
|
627
|
+
Full TypeScript support:
|
|
453
628
|
|
|
454
629
|
```typescript
|
|
455
630
|
import type {
|
|
456
631
|
User,
|
|
457
632
|
LoginResponse,
|
|
458
|
-
Permission,
|
|
459
|
-
Role,
|
|
460
|
-
DataScope,
|
|
461
633
|
Session,
|
|
462
634
|
OAuthBinding,
|
|
463
635
|
} from "@amaster.ai/auth-client";
|
|
@@ -470,3 +642,4 @@ MIT
|
|
|
470
642
|
## Contributing
|
|
471
643
|
|
|
472
644
|
Contributions are welcome! Please read our contributing guidelines before submitting PRs.
|
|
645
|
+
````
|
package/dist/auth.d.cts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* ============================================================================
|
|
15
15
|
*/
|
|
16
16
|
import { HttpClient, ClientResult } from '@amaster.ai/http-client';
|
|
17
|
-
import {
|
|
17
|
+
import { q as User, i as RegisterParams, e as LoginResponse, L as LoginParams, c as CodeLoginParams, S as SendCodeParams, p as SuccessResponse, b as CaptchaResponse, g as OAuthProvider, M as MiniProgramPhoneResponse, R as RefreshTokenResponse } from './types-DGF9cpAg.cjs';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Authentication Module
|
|
@@ -30,11 +30,16 @@ import { s as User, k as RegisterParams, f as LoginResponse, L as LoginParams, d
|
|
|
30
30
|
* - Logout and token refresh
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
|
+
declare const browserNavigation: {
|
|
34
|
+
replace(url: string): void;
|
|
35
|
+
};
|
|
33
36
|
interface AuthModuleDeps {
|
|
34
37
|
http: HttpClient;
|
|
35
38
|
onLoginSuccess: (user: User, accessToken: string) => void;
|
|
39
|
+
autoRedirectAfterLogin: boolean;
|
|
36
40
|
storage: {
|
|
37
41
|
getItem: (key: string) => string | null;
|
|
42
|
+
setItem: (key: string, value: string) => void;
|
|
38
43
|
};
|
|
39
44
|
clearAuth: () => void;
|
|
40
45
|
}
|
|
@@ -59,8 +64,14 @@ declare function createAuthModule(deps: AuthModuleDeps): {
|
|
|
59
64
|
* @category Authentication
|
|
60
65
|
* @example
|
|
61
66
|
* ```typescript
|
|
67
|
+
* // Auto-detect loginType from username
|
|
68
|
+
* await auth.login({
|
|
69
|
+
* username: "john_doe",
|
|
70
|
+
* password: "Password@123",
|
|
71
|
+
* });
|
|
72
|
+
*
|
|
73
|
+
* // Auto-detect loginType from email
|
|
62
74
|
* await auth.login({
|
|
63
|
-
* loginType: "email",
|
|
64
75
|
* email: "user@example.com",
|
|
65
76
|
* password: "Password@123",
|
|
66
77
|
* });
|
|
@@ -73,11 +84,17 @@ declare function createAuthModule(deps: AuthModuleDeps): {
|
|
|
73
84
|
* @category Authentication
|
|
74
85
|
* @example
|
|
75
86
|
* ```typescript
|
|
87
|
+
* // Auto-detect loginType from email
|
|
76
88
|
* await auth.loginWithCode({
|
|
77
|
-
* loginType: "email",
|
|
78
89
|
* email: "user@example.com",
|
|
79
90
|
* code: "123456",
|
|
80
91
|
* });
|
|
92
|
+
*
|
|
93
|
+
* // Auto-detect loginType from phone
|
|
94
|
+
* await auth.loginWithCode({
|
|
95
|
+
* phone: "13800138000",
|
|
96
|
+
* code: "123456",
|
|
97
|
+
* });
|
|
81
98
|
* ```
|
|
82
99
|
*/
|
|
83
100
|
loginWithCode(params: CodeLoginParams): Promise<ClientResult<LoginResponse>>;
|
|
@@ -116,6 +133,51 @@ declare function createAuthModule(deps: AuthModuleDeps): {
|
|
|
116
133
|
* @category Authentication
|
|
117
134
|
*/
|
|
118
135
|
handleOAuthCallback(): Promise<ClientResult<LoginResponse>>;
|
|
136
|
+
/**
|
|
137
|
+
* Login with WeChat Mini Program code
|
|
138
|
+
*
|
|
139
|
+
* @category Authentication
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* // In WeChat Mini Program
|
|
143
|
+
* wx.login({
|
|
144
|
+
* success: async (res) => {
|
|
145
|
+
* if (res.code) {
|
|
146
|
+
* const result = await auth.loginWithMiniProgram(res.code);
|
|
147
|
+
* if (result.data) {
|
|
148
|
+
* console.log("Logged in:", result.data.user);
|
|
149
|
+
* }
|
|
150
|
+
* }
|
|
151
|
+
* }
|
|
152
|
+
* });
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
loginWithMiniProgram(code: string): Promise<ClientResult<LoginResponse>>;
|
|
156
|
+
/**
|
|
157
|
+
* Get WeChat Mini Program user phone number
|
|
158
|
+
* Requires user authorization via getPhoneNumber button
|
|
159
|
+
*
|
|
160
|
+
* @category Authentication
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* // WXML
|
|
164
|
+
* <button open-type="getPhoneNumber" bindgetphonenumber="onGetPhoneNumber">
|
|
165
|
+
* Get Phone Number
|
|
166
|
+
* </button>
|
|
167
|
+
*
|
|
168
|
+
* // JS
|
|
169
|
+
* async onGetPhoneNumber(e) {
|
|
170
|
+
* const { code } = e.detail;
|
|
171
|
+
* if (code) {
|
|
172
|
+
* const result = await auth.getMiniProgramPhoneNumber(code);
|
|
173
|
+
* if (result.data) {
|
|
174
|
+
* console.log("Phone:", result.data.phone);
|
|
175
|
+
* }
|
|
176
|
+
* }
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
getMiniProgramPhoneNumber(code: string): Promise<ClientResult<MiniProgramPhoneResponse>>;
|
|
119
181
|
/**
|
|
120
182
|
* Logout current user
|
|
121
183
|
*
|
|
@@ -135,4 +197,4 @@ declare function createAuthModule(deps: AuthModuleDeps): {
|
|
|
135
197
|
};
|
|
136
198
|
type AuthModule = ReturnType<typeof createAuthModule>;
|
|
137
199
|
|
|
138
|
-
export { type AuthModule, type AuthModuleDeps, createAuthModule };
|
|
200
|
+
export { type AuthModule, type AuthModuleDeps, browserNavigation, createAuthModule };
|
package/dist/auth.d.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* ============================================================================
|
|
15
15
|
*/
|
|
16
16
|
import { HttpClient, ClientResult } from '@amaster.ai/http-client';
|
|
17
|
-
import {
|
|
17
|
+
import { q as User, i as RegisterParams, e as LoginResponse, L as LoginParams, c as CodeLoginParams, S as SendCodeParams, p as SuccessResponse, b as CaptchaResponse, g as OAuthProvider, M as MiniProgramPhoneResponse, R as RefreshTokenResponse } from './types-DGF9cpAg.js';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Authentication Module
|
|
@@ -30,11 +30,16 @@ import { s as User, k as RegisterParams, f as LoginResponse, L as LoginParams, d
|
|
|
30
30
|
* - Logout and token refresh
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
|
+
declare const browserNavigation: {
|
|
34
|
+
replace(url: string): void;
|
|
35
|
+
};
|
|
33
36
|
interface AuthModuleDeps {
|
|
34
37
|
http: HttpClient;
|
|
35
38
|
onLoginSuccess: (user: User, accessToken: string) => void;
|
|
39
|
+
autoRedirectAfterLogin: boolean;
|
|
36
40
|
storage: {
|
|
37
41
|
getItem: (key: string) => string | null;
|
|
42
|
+
setItem: (key: string, value: string) => void;
|
|
38
43
|
};
|
|
39
44
|
clearAuth: () => void;
|
|
40
45
|
}
|
|
@@ -59,8 +64,14 @@ declare function createAuthModule(deps: AuthModuleDeps): {
|
|
|
59
64
|
* @category Authentication
|
|
60
65
|
* @example
|
|
61
66
|
* ```typescript
|
|
67
|
+
* // Auto-detect loginType from username
|
|
68
|
+
* await auth.login({
|
|
69
|
+
* username: "john_doe",
|
|
70
|
+
* password: "Password@123",
|
|
71
|
+
* });
|
|
72
|
+
*
|
|
73
|
+
* // Auto-detect loginType from email
|
|
62
74
|
* await auth.login({
|
|
63
|
-
* loginType: "email",
|
|
64
75
|
* email: "user@example.com",
|
|
65
76
|
* password: "Password@123",
|
|
66
77
|
* });
|
|
@@ -73,11 +84,17 @@ declare function createAuthModule(deps: AuthModuleDeps): {
|
|
|
73
84
|
* @category Authentication
|
|
74
85
|
* @example
|
|
75
86
|
* ```typescript
|
|
87
|
+
* // Auto-detect loginType from email
|
|
76
88
|
* await auth.loginWithCode({
|
|
77
|
-
* loginType: "email",
|
|
78
89
|
* email: "user@example.com",
|
|
79
90
|
* code: "123456",
|
|
80
91
|
* });
|
|
92
|
+
*
|
|
93
|
+
* // Auto-detect loginType from phone
|
|
94
|
+
* await auth.loginWithCode({
|
|
95
|
+
* phone: "13800138000",
|
|
96
|
+
* code: "123456",
|
|
97
|
+
* });
|
|
81
98
|
* ```
|
|
82
99
|
*/
|
|
83
100
|
loginWithCode(params: CodeLoginParams): Promise<ClientResult<LoginResponse>>;
|
|
@@ -116,6 +133,51 @@ declare function createAuthModule(deps: AuthModuleDeps): {
|
|
|
116
133
|
* @category Authentication
|
|
117
134
|
*/
|
|
118
135
|
handleOAuthCallback(): Promise<ClientResult<LoginResponse>>;
|
|
136
|
+
/**
|
|
137
|
+
* Login with WeChat Mini Program code
|
|
138
|
+
*
|
|
139
|
+
* @category Authentication
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* // In WeChat Mini Program
|
|
143
|
+
* wx.login({
|
|
144
|
+
* success: async (res) => {
|
|
145
|
+
* if (res.code) {
|
|
146
|
+
* const result = await auth.loginWithMiniProgram(res.code);
|
|
147
|
+
* if (result.data) {
|
|
148
|
+
* console.log("Logged in:", result.data.user);
|
|
149
|
+
* }
|
|
150
|
+
* }
|
|
151
|
+
* }
|
|
152
|
+
* });
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
loginWithMiniProgram(code: string): Promise<ClientResult<LoginResponse>>;
|
|
156
|
+
/**
|
|
157
|
+
* Get WeChat Mini Program user phone number
|
|
158
|
+
* Requires user authorization via getPhoneNumber button
|
|
159
|
+
*
|
|
160
|
+
* @category Authentication
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* // WXML
|
|
164
|
+
* <button open-type="getPhoneNumber" bindgetphonenumber="onGetPhoneNumber">
|
|
165
|
+
* Get Phone Number
|
|
166
|
+
* </button>
|
|
167
|
+
*
|
|
168
|
+
* // JS
|
|
169
|
+
* async onGetPhoneNumber(e) {
|
|
170
|
+
* const { code } = e.detail;
|
|
171
|
+
* if (code) {
|
|
172
|
+
* const result = await auth.getMiniProgramPhoneNumber(code);
|
|
173
|
+
* if (result.data) {
|
|
174
|
+
* console.log("Phone:", result.data.phone);
|
|
175
|
+
* }
|
|
176
|
+
* }
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
getMiniProgramPhoneNumber(code: string): Promise<ClientResult<MiniProgramPhoneResponse>>;
|
|
119
181
|
/**
|
|
120
182
|
* Logout current user
|
|
121
183
|
*
|
|
@@ -135,4 +197,4 @@ declare function createAuthModule(deps: AuthModuleDeps): {
|
|
|
135
197
|
};
|
|
136
198
|
type AuthModule = ReturnType<typeof createAuthModule>;
|
|
137
199
|
|
|
138
|
-
export { type AuthModule, type AuthModuleDeps, createAuthModule };
|
|
200
|
+
export { type AuthModule, type AuthModuleDeps, browserNavigation, createAuthModule };
|