@amaster.ai/client 1.1.3 → 1.1.5

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,22 +1,22 @@
1
1
  /**
2
2
  * Authentication module - provides user authentication and management capabilities.
3
- *
3
+ *
4
4
  * This module exports all auth-related types for convenient importing:
5
- *
5
+ *
6
6
  * @example
7
7
  * // Import specific types
8
8
  * import type { LoginParams, User, OAuthProvider } from '@amaster.ai/client/auth';
9
- *
9
+ *
10
10
  * @example
11
11
  * // Import multiple types
12
- * import type {
13
- * LoginParams,
12
+ * import type {
13
+ * LoginParams,
14
14
  * RegisterParams,
15
15
  * User,
16
16
  * Role,
17
- * Permission
17
+ * Permission
18
18
  * } from '@amaster.ai/client/auth';
19
- *
19
+ *
20
20
  * @module auth
21
21
  * @since 1.0.0
22
22
  */
@@ -28,138 +28,118 @@ export * from './user';
28
28
  export * from './password-auth';
29
29
  export * from './code-auth';
30
30
  export * from './oauth';
31
+ export * from './permissions';
31
32
  export * from './profile';
33
+ export * from './sessions';
32
34
 
33
35
  // Import for unified API
34
- import type { PasswordAuthAPI } from './password-auth';
36
+ import type { PasswordAuthAPI, RefreshTokenResponse, SuccessResponse } from './password-auth';
35
37
  import type { CodeAuthAPI } from './code-auth';
36
38
  import type { OAuthAPI } from './oauth';
39
+ import type { PermissionsAPI } from './permissions';
37
40
  import type { ProfileAPI } from './profile';
41
+ import type { SessionsAPI } from './sessions';
38
42
 
39
- // ==================== Token Management ====================
43
+ // ==================== Events ====================
40
44
 
41
45
  /**
42
- * Token refresh response
46
+ * Authentication lifecycle events
43
47
  */
44
- export interface RefreshTokenResponse {
45
- /** New access token */
46
- accessToken: string;
47
-
48
- /** Token expiration time in seconds */
49
- expiresIn?: number;
50
- }
48
+ export type AuthEvent =
49
+ | 'login'
50
+ | 'logout'
51
+ | 'tokenExpired'
52
+ | 'tokenRefreshed'
53
+ | 'unauthorized';
51
54
 
52
55
  /**
53
- * Generic success response
56
+ * Event callback signature
54
57
  */
55
- export interface SuccessResponse {
56
- /** Whether operation was successful */
57
- success: boolean;
58
-
59
- /** Optional message */
60
- message?: string;
61
- }
58
+ export type EventHandler = (...args: any[]) => void;
62
59
 
63
60
  // ==================== Unified Authentication API ====================
64
61
 
65
62
  /**
66
63
  * Complete Authentication Client API
67
- *
64
+ *
68
65
  * Combines all authentication capabilities into a single interface.
69
66
  * All methods return a `ClientResult<T>` for consistent error handling.
70
- *
71
67
  */
72
- export interface AuthClientAPI
68
+ export interface AuthClientAPI
73
69
  extends PasswordAuthAPI,
74
70
  CodeAuthAPI,
75
71
  OAuthAPI,
76
- ProfileAPI {
77
-
72
+ PermissionsAPI,
73
+ ProfileAPI,
74
+ SessionsAPI {
75
+
78
76
  /**
79
77
  * Logout current user
80
- *
78
+ *
81
79
  * Invalidates the current session and clears local authentication state.
82
80
  * All subsequent requests will be unauthenticated until login again.
83
- *
84
- * @returns Success status
85
81
  *
82
+ * @returns Success status
86
83
  */
87
84
  logout(): Promise<ClientResult<SuccessResponse>>;
88
85
 
89
86
  /**
90
87
  * Refresh access token
91
- *
88
+ *
92
89
  * Obtains a new access token using the refresh token.
93
90
  * Note: Token refresh is handled automatically by the client.
94
91
  * This method is exposed for manual refresh scenarios.
95
- *
96
- * @returns New access token
97
92
  *
93
+ * @returns New access token
98
94
  */
99
95
  refreshToken(): Promise<ClientResult<RefreshTokenResponse>>;
100
96
 
101
- // ==================== Event Handling ====================
102
-
103
97
  /**
104
98
  * Subscribe to authentication events
105
- *
99
+ *
106
100
  * Available events:
107
101
  * - `login` - Fired after successful login, receives User object
108
102
  * - `logout` - Fired after logout
109
103
  * - `tokenExpired` - Fired when access token expires
110
104
  * - `tokenRefreshed` - Fired after token refresh, receives new token
111
105
  * - `unauthorized` - Fired on 401 response
112
- *
106
+ *
113
107
  * @param event - Event name to subscribe to
114
108
  * @param handler - Callback function
115
- *
116
109
  */
117
- on(event: "login" | "logout" | "tokenExpired" | "tokenRefreshed" | "unauthorized", handler: (...args: unknown[]) => void): void;
110
+ on(event: AuthEvent, handler: EventHandler): void;
118
111
 
119
112
  /**
120
113
  * Unsubscribe from authentication events
121
- *
114
+ *
122
115
  * @param event - Event name to unsubscribe from
123
116
  * @param handler - The same callback function that was passed to `on()`
124
- *
125
117
  */
126
- off(event: "login" | "logout" | "tokenExpired" | "tokenRefreshed" | "unauthorized", handler: (...args: unknown[]) => void): void;
118
+ off(event: AuthEvent, handler: EventHandler): void;
127
119
 
128
- // ==================== Permission Checks ====================
129
-
130
120
  /**
131
- * Check if current user has a specific role (local check, fast)
132
- *
133
- * Works for both authenticated and anonymous users.
134
- * Checks against locally cached user roles.
135
- *
136
- * @param roleCode - Role code to check (e.g., "admin", "user", "anonymous")
137
- * @returns True if user has the role
121
+ * Check whether the current client has a stored access token.
138
122
  *
123
+ * @returns True when authenticated, false otherwise
139
124
  */
140
- hasRole(roleCode: string): boolean;
141
-
125
+ isAuthenticated(): boolean;
126
+
142
127
  /**
143
- * Check if current user has a specific permission (local check, fast)
144
- *
145
- * Works for both authenticated and anonymous users.
146
- * Checks against locally cached user permissions.
147
- *
148
- * @param resource - Resource name (e.g., "user", "order")
149
- * @param action - Action name (e.g., "read", "write", "delete")
150
- * @returns True if user has the permission
128
+ * Get the current access token from local storage.
151
129
  *
130
+ * @returns Access token string, or null when not authenticated
152
131
  */
153
- hasPermission(resource: string, action: string): boolean;
154
-
132
+ getAccessToken(): string | null;
133
+
155
134
  /**
156
- * Check if current user is anonymous (not authenticated)
157
- *
158
- * Convenience method equivalent to `hasRole('anonymous')`.
159
- * Returns true if user has the 'anonymous' role.
160
- *
161
- * @returns True if user is anonymous
135
+ * Manually set the current access token.
162
136
  *
137
+ * @param token - JWT access token
138
+ */
139
+ setAccessToken(token: string): void;
140
+
141
+ /**
142
+ * Clear all local authentication state, including token and cached user info.
163
143
  */
164
- isAnonymous(): boolean;
144
+ clearAuth(): void;
165
145
  }
@@ -1,143 +1,194 @@
1
1
  /**
2
2
  * * OAuth and social authentication including:
3
- * - GitHub OAuth
4
- * - Google OAuth
5
- * - WeChat OAuth (Web & Mini Program)
6
- * - Generic OAuth flow
7
- *
3
+ * - Browser OAuth login
4
+ * - OAuth callback handling
5
+ * - Mini Program login
6
+ * - OAuth account binding management
7
+ *
8
8
  * @module auth/oauth
9
9
  */
10
10
 
11
11
  import type { ClientResult } from '../common';
12
- import type { LoginResponse } from './password-auth';
12
+ import type { LoginResponse, SuccessResponse } from './password-auth';
13
13
 
14
14
  // ==================== OAuth Providers ====================
15
15
 
16
16
  /**
17
17
  * Supported OAuth providers
18
18
  */
19
- export type OAuthProvider =
20
- | 'github'
19
+ export type OAuthProvider =
21
20
  | 'google'
21
+ | 'github'
22
22
  | 'wechat'
23
- | 'wechat-miniprogram'
24
- | 'gitlab'
25
- | 'microsoft';
23
+ /** @deprecated Legacy mini-program provider marker kept for compatibility */
24
+ | 'wechat_mini'
25
+ | 'platform';
26
26
 
27
27
  /**
28
- * OAuth login parameters
29
- *
28
+ * OAuth binding information for the current user
30
29
  */
31
- export interface OAuthLoginParams {
30
+ export interface OAuthBinding {
32
31
  /** OAuth provider */
33
32
  provider: OAuthProvider;
34
-
35
- /** Authorization code from OAuth callback */
36
- code: string;
37
-
38
- /** Redirect URI used in the OAuth flow */
39
- redirectUri?: string;
40
-
41
- /** State parameter for CSRF protection */
42
- state?: string;
33
+
34
+ /** Provider-specific user ID */
35
+ providerId: string;
36
+
37
+ /** OAuth account email */
38
+ email: string;
39
+
40
+ /** Display name from provider */
41
+ displayName: string;
42
+
43
+ /** Provider avatar URL */
44
+ avatarUrl: string | null;
45
+
46
+ /** Binding creation timestamp */
47
+ createdAt: string;
43
48
  }
44
49
 
50
+ // ==================== Mini Program ====================
51
+
45
52
  /**
46
- * Get OAuth URL parameters
53
+ * Supported Mini Program platforms
47
54
  */
48
- export interface GetOAuthUrlParams {
49
- /** OAuth provider */
50
- provider: OAuthProvider;
51
-
52
- /** Redirect URI after OAuth */
53
- redirectUri: string;
54
-
55
- /** State parameter for CSRF protection */
56
- state?: string;
57
-
58
- /** Additional scopes (provider-specific) */
59
- scopes?: string[];
60
- }
55
+ export type MiniProgramPlatform = 'wechat' | 'douyin';
61
56
 
62
57
  /**
63
- * OAuth URL response
58
+ * Unified Mini Program login options
64
59
  */
65
- export interface OAuthUrlResponse {
66
- /** Authorization URL to redirect user to */
67
- authUrl: string;
68
-
69
- /** State parameter for verification */
70
- state: string;
60
+ export interface MiniLoginParams {
61
+ /** Optional explicit platform override */
62
+ platform?: MiniProgramPlatform;
71
63
  }
72
64
 
73
- // ==================== WeChat Specific ====================
74
-
75
65
  /**
76
- * WeChat Mini Program login parameters
77
- *
66
+ * Mini Program login parameters
78
67
  */
79
- export interface WeChatMiniProgramLoginParams {
80
- /** WeChat login code */
68
+ export interface MiniProgramLoginParams {
69
+ /** Code from mini-program login API */
81
70
  code: string;
82
-
83
- /** User info from WeChat (optional) */
84
- userInfo?: {
85
- /** Encrypted user data */
86
- encryptedData: string;
87
- /** IV for decryption */
88
- iv: string;
89
- };
71
+ platform?: MiniProgramPlatform;
90
72
  }
91
73
 
92
74
  /**
93
- * WeChat web OAuth parameters
75
+ * Mini Program phone number parameters
94
76
  */
95
- export interface WeChatWebLoginParams {
96
- /** Authorization code from WeChat OAuth */
77
+ export interface MiniProgramPhoneParams {
78
+ /** Code from `getPhoneNumber` button event */
97
79
  code: string;
98
-
99
- /** State parameter */
100
- state?: string;
80
+ platform?: MiniProgramPlatform;
81
+ }
82
+
83
+ /**
84
+ * Mini Program phone event-like payload
85
+ */
86
+ export interface MiniGetPhoneEvent {
87
+ code?: string;
88
+ platform?: MiniProgramPlatform;
89
+ detail?: {
90
+ code?: string;
91
+ } | null;
92
+ }
93
+
94
+ /**
95
+ * Unified Mini Program phone input
96
+ */
97
+ export type MiniGetPhoneParams =
98
+ | string
99
+ | MiniProgramPhoneParams
100
+ | MiniGetPhoneEvent;
101
+
102
+ /**
103
+ * Mini Program phone number response
104
+ */
105
+ export interface MiniProgramPhoneResponse {
106
+ /** Phone number with country code */
107
+ phone: string;
108
+
109
+ /** Whether the phone number is verified by WeChat */
110
+ phoneVerified: boolean;
101
111
  }
102
112
 
103
113
  // ==================== API ====================
104
114
 
105
115
  /**
106
116
  * OAuth & Social Login API
107
- *
108
- * Methods for OAuth-based authentication.
117
+ *
118
+ * Methods for OAuth-based authentication and Mini Program login.
109
119
  */
110
120
  export interface OAuthAPI {
111
121
  /**
112
- * Get OAuth authorization URL
113
- *
114
- * Generates the URL to redirect users to for OAuth authentication.
115
- *
116
- * @param params - OAuth provider and redirect URI
117
- * @returns Authorization URL and state
122
+ * Redirect to OAuth provider for authentication
118
123
  *
124
+ * @param provider - OAuth provider name
125
+ * @param redirectUrl - Optional post-login redirect target
119
126
  */
120
- getOAuthUrl(params: GetOAuthUrlParams): Promise<ClientResult<OAuthUrlResponse>>;
127
+ loginWithOAuth(provider: OAuthProvider, redirectUrl?: string): void;
121
128
 
122
129
  /**
123
- * Complete OAuth login
124
- *
125
- * Exchanges the OAuth authorization code for access token and user info.
126
- *
127
- * @param params - OAuth provider and authorization code
128
- * @returns User info and access token
130
+ * Handle OAuth callback on the current browser page
129
131
  *
132
+ * Parses the callback hash, stores the access token, and returns the current user.
130
133
  */
131
- oauthLogin(params: OAuthLoginParams): Promise<ClientResult<LoginResponse>>;
134
+ handleOAuthCallback(): Promise<ClientResult<LoginResponse>>;
132
135
 
133
136
  /**
134
- * WeChat Mini Program login
135
- *
136
- * Authenticates user in WeChat Mini Program environment.
137
- *
138
- * @param params - WeChat login code
137
+ * Login with Mini Program code
138
+ *
139
+ * Lower-level API for submitting an already acquired login code.
140
+ *
141
+ * @deprecated Prefer `miniLogin()` unless you already have a resolved mini-program login code.
142
+ *
143
+ * @param code - Code returned by the current mini-program runtime
139
144
  * @returns User info and access token
145
+ */
146
+ loginWithMiniProgram(code: string | MiniProgramLoginParams): Promise<ClientResult<LoginResponse>>;
147
+
148
+ /**
149
+ * Unified Mini Program login entry
150
+ *
151
+ * Automatically fetches the login code from Taro / wx / tt and submits it to the backend.
152
+ */
153
+ miniLogin(params?: MiniLoginParams): Promise<ClientResult<LoginResponse>>;
154
+
155
+ /**
156
+ * Get Mini Program user phone number
157
+ *
158
+ * Lower-level API for submitting an already acquired phone code.
159
+ *
160
+ * @deprecated Prefer `miniGetPhone(...)` unless you already have a resolved phone authorization code.
161
+ *
162
+ * @param code - Code returned by the Mini Program phone-number event
163
+ * @returns Phone number info
164
+ */
165
+ getMiniProgramPhoneNumber(
166
+ code: string | MiniProgramPhoneParams
167
+ ): Promise<ClientResult<MiniProgramPhoneResponse>>;
168
+
169
+ /**
170
+ * Unified Mini Program phone binding entry
171
+ *
172
+ * Accepts a raw code, `{ code, platform }`, or an event-like object containing `detail.code`.
173
+ */
174
+ miniGetPhone(input: MiniGetPhoneParams): Promise<ClientResult<MiniProgramPhoneResponse>>;
175
+
176
+ /**
177
+ * Get OAuth accounts currently bound to the logged-in user
178
+ */
179
+ getOAuthBindings(): Promise<ClientResult<OAuthBinding[]>>;
180
+
181
+ /**
182
+ * Redirect to provider binding flow for the current user
183
+ *
184
+ * @param provider - OAuth provider name
185
+ */
186
+ bindOAuth(provider: OAuthProvider): void;
187
+
188
+ /**
189
+ * Unbind an OAuth account from the current user
140
190
  *
191
+ * @param provider - OAuth provider name
141
192
  */
142
- wechatMiniProgramLogin(params: WeChatMiniProgramLoginParams): Promise<ClientResult<LoginResponse>>;
193
+ unbindOAuth(provider: OAuthProvider): Promise<ClientResult<SuccessResponse>>;
143
194
  }