@amaster.ai/client 1.0.0-alpha.1

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.
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Authentication module - provides user authentication and management capabilities.
3
+ *
4
+ * This module exports all auth-related types for convenient importing:
5
+ *
6
+ * @example
7
+ * // Import specific types
8
+ * import type { LoginParams, User, OAuthProvider } from '@amaster.ai/client/auth';
9
+ *
10
+ * @example
11
+ * // Import multiple types
12
+ * import type {
13
+ * LoginParams,
14
+ * RegisterParams,
15
+ * User,
16
+ * Role,
17
+ * Permission
18
+ * } from '@amaster.ai/client/auth';
19
+ *
20
+ * @module auth
21
+ * @since 1.0.0
22
+ */
23
+
24
+ import type { ClientResult } from '../common';
25
+
26
+ // Re-export all submodule types for convenient importing
27
+ export * from './user';
28
+ export * from './password-auth';
29
+ export * from './code-auth';
30
+ export * from './oauth';
31
+ export * from './profile';
32
+
33
+ // Import for unified API
34
+ import type { PasswordAuthAPI } from './password-auth';
35
+ import type { CodeAuthAPI } from './code-auth';
36
+ import type { OAuthAPI } from './oauth';
37
+ import type { ProfileAPI } from './profile';
38
+
39
+ // ==================== Token Management ====================
40
+
41
+ /**
42
+ * Token refresh response
43
+ */
44
+ export interface RefreshTokenResponse {
45
+ /** New access token */
46
+ accessToken: string;
47
+
48
+ /** Token expiration time in seconds */
49
+ expiresIn?: number;
50
+ }
51
+
52
+ /**
53
+ * Generic success response
54
+ */
55
+ export interface SuccessResponse {
56
+ /** Whether operation was successful */
57
+ success: boolean;
58
+
59
+ /** Optional message */
60
+ message?: string;
61
+ }
62
+
63
+ // ==================== Unified Authentication API ====================
64
+
65
+ /**
66
+ * Complete Authentication Client API
67
+ *
68
+ * Combines all authentication capabilities into a single interface.
69
+ * All methods return a `ClientResult<T>` for consistent error handling.
70
+ *
71
+ */
72
+ export interface AuthClientAPI
73
+ extends PasswordAuthAPI,
74
+ CodeAuthAPI,
75
+ OAuthAPI,
76
+ ProfileAPI {
77
+
78
+ /**
79
+ * Logout current user
80
+ *
81
+ * Invalidates the current session and clears local authentication state.
82
+ * All subsequent requests will be unauthenticated until login again.
83
+ *
84
+ * @returns Success status
85
+ *
86
+ */
87
+ logout(): Promise<ClientResult<SuccessResponse>>;
88
+
89
+ /**
90
+ * Refresh access token
91
+ *
92
+ * Obtains a new access token using the refresh token.
93
+ * Note: Token refresh is handled automatically by the client.
94
+ * This method is exposed for manual refresh scenarios.
95
+ *
96
+ * @returns New access token
97
+ *
98
+ */
99
+ refreshToken(): Promise<ClientResult<RefreshTokenResponse>>;
100
+
101
+ // ==================== Permission Checks ====================
102
+
103
+ /**
104
+ * Check if current user has a specific role (local check, fast)
105
+ *
106
+ * Works for both authenticated and anonymous users.
107
+ * Checks against locally cached user roles.
108
+ *
109
+ * @param roleCode - Role code to check (e.g., "admin", "user", "anonymous")
110
+ * @returns True if user has the role
111
+ *
112
+ */
113
+ hasRole(roleCode: string): boolean;
114
+
115
+ /**
116
+ * Check if current user has a specific permission (local check, fast)
117
+ *
118
+ * Works for both authenticated and anonymous users.
119
+ * Checks against locally cached user permissions.
120
+ *
121
+ * @param resource - Resource name (e.g., "user", "order")
122
+ * @param action - Action name (e.g., "read", "write", "delete")
123
+ * @returns True if user has the permission
124
+ *
125
+ */
126
+ hasPermission(resource: string, action: string): boolean;
127
+
128
+ /**
129
+ * Check if current user is anonymous (not authenticated)
130
+ *
131
+ * Convenience method equivalent to `hasRole('anonymous')`.
132
+ * Returns true if user has the 'anonymous' role.
133
+ *
134
+ * @returns True if user is anonymous
135
+ *
136
+ */
137
+ isAnonymous(): boolean;
138
+ }
@@ -0,0 +1,143 @@
1
+ /**
2
+ * * OAuth and social authentication including:
3
+ * - GitHub OAuth
4
+ * - Google OAuth
5
+ * - WeChat OAuth (Web & Mini Program)
6
+ * - Generic OAuth flow
7
+ *
8
+ * @module auth/oauth
9
+ */
10
+
11
+ import type { ClientResult } from '../common';
12
+ import type { LoginResponse } from './password-auth';
13
+
14
+ // ==================== OAuth Providers ====================
15
+
16
+ /**
17
+ * Supported OAuth providers
18
+ */
19
+ export type OAuthProvider =
20
+ | 'github'
21
+ | 'google'
22
+ | 'wechat'
23
+ | 'wechat-miniprogram'
24
+ | 'gitlab'
25
+ | 'microsoft';
26
+
27
+ /**
28
+ * OAuth login parameters
29
+ *
30
+ */
31
+ export interface OAuthLoginParams {
32
+ /** OAuth provider */
33
+ 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;
43
+ }
44
+
45
+ /**
46
+ * Get OAuth URL parameters
47
+ */
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
+ }
61
+
62
+ /**
63
+ * OAuth URL response
64
+ */
65
+ export interface OAuthUrlResponse {
66
+ /** Authorization URL to redirect user to */
67
+ authUrl: string;
68
+
69
+ /** State parameter for verification */
70
+ state: string;
71
+ }
72
+
73
+ // ==================== WeChat Specific ====================
74
+
75
+ /**
76
+ * WeChat Mini Program login parameters
77
+ *
78
+ */
79
+ export interface WeChatMiniProgramLoginParams {
80
+ /** WeChat login code */
81
+ 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
+ };
90
+ }
91
+
92
+ /**
93
+ * WeChat web OAuth parameters
94
+ */
95
+ export interface WeChatWebLoginParams {
96
+ /** Authorization code from WeChat OAuth */
97
+ code: string;
98
+
99
+ /** State parameter */
100
+ state?: string;
101
+ }
102
+
103
+ // ==================== API ====================
104
+
105
+ /**
106
+ * OAuth & Social Login API
107
+ *
108
+ * Methods for OAuth-based authentication.
109
+ */
110
+ export interface OAuthAPI {
111
+ /**
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
118
+ *
119
+ */
120
+ getOAuthUrl(params: GetOAuthUrlParams): Promise<ClientResult<OAuthUrlResponse>>;
121
+
122
+ /**
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
129
+ *
130
+ */
131
+ oauthLogin(params: OAuthLoginParams): Promise<ClientResult<LoginResponse>>;
132
+
133
+ /**
134
+ * WeChat Mini Program login
135
+ *
136
+ * Authenticates user in WeChat Mini Program environment.
137
+ *
138
+ * @param params - WeChat login code
139
+ * @returns User info and access token
140
+ *
141
+ */
142
+ wechatMiniProgramLogin(params: WeChatMiniProgramLoginParams): Promise<ClientResult<LoginResponse>>;
143
+ }
@@ -0,0 +1,226 @@
1
+ /**
2
+ * * Password-based authentication including:
3
+ * - Email/username/phone + password login
4
+ * - User registration
5
+ * - Password change
6
+ *
7
+ * @module auth/password-auth
8
+ */
9
+
10
+ import type { ClientResult } from '../common';
11
+ import type { User } from './user';
12
+
13
+ // ==================== Parameters ====================
14
+
15
+ /**
16
+ * User registration parameters
17
+ *
18
+ * At least one identifier (username, email, or phone) must be provided.
19
+ *
20
+ */
21
+ export interface RegisterParams {
22
+ /** Username (optional, but one of username/email/phone required) */
23
+ username?: string;
24
+
25
+ /** Email address (optional, but one of username/email/phone required) */
26
+ email?: string;
27
+
28
+ /** Phone number (optional, but one of username/email/phone required) */
29
+ phone?: string;
30
+
31
+ /**
32
+ * Password (required, 8-128 characters)
33
+ * Must contain at least: uppercase, lowercase, number
34
+ */
35
+ password: string;
36
+
37
+ /** Display name for UI */
38
+ displayName?: string;
39
+
40
+ /**
41
+ * Captcha verification (optional)
42
+ * Format: "captchaId:userInput"
43
+ */
44
+ captcha?: string;
45
+ }
46
+
47
+ /**
48
+ * Login method type
49
+ */
50
+ export type LoginType = 'username' | 'email' | 'phone';
51
+
52
+ /**
53
+ * Login parameters for password-based authentication
54
+ *
55
+ * The `loginType` field is optional - it will be auto-detected based on
56
+ * which identifier field (username/email/phone) you provide.
57
+ *
58
+ */
59
+ export interface LoginParams {
60
+ /** Login method (optional, auto-detected if not provided) */
61
+ loginType?: LoginType;
62
+
63
+ /** Username (required if loginType='username') */
64
+ username?: string;
65
+
66
+ /** Email (required if loginType='email') */
67
+ email?: string;
68
+
69
+ /** Phone (required if loginType='phone') */
70
+ phone?: string;
71
+
72
+ /** Password (always required) */
73
+ password: string;
74
+ }
75
+
76
+ /**
77
+ * Change password parameters
78
+ *
79
+ */
80
+ export interface ChangePasswordParams {
81
+ /** Current password (for verification) */
82
+ oldPassword: string;
83
+
84
+ /**
85
+ * New password (8-128 characters)
86
+ * Must contain: uppercase, lowercase, number
87
+ */
88
+ newPassword: string;
89
+ }
90
+
91
+ // ==================== Responses ====================
92
+
93
+ /**
94
+ * Login/Registration response with user info and access token
95
+ *
96
+ */
97
+ export interface LoginResponse {
98
+ /** User information */
99
+ user: User;
100
+
101
+ /** JWT access token */
102
+ accessToken: string;
103
+
104
+ /** Refresh token (if configured) */
105
+ refreshToken?: string;
106
+
107
+ /** Token expiration time in seconds */
108
+ expiresIn?: number;
109
+ }
110
+
111
+ /**
112
+ * Generic success response
113
+ */
114
+ export interface SuccessResponse {
115
+ /** Whether operation was successful */
116
+ success: boolean;
117
+
118
+ /** Optional message */
119
+ message?: string;
120
+ }
121
+
122
+ // ==================== API ====================
123
+
124
+ /**
125
+ * Password Authentication API
126
+ *
127
+ * Methods for password-based authentication and account management.
128
+ *
129
+ * @since 1.0.0
130
+ */
131
+ export interface PasswordAuthAPI {
132
+ /**
133
+ * Register a new user account
134
+ *
135
+ * Creates a new user account with the provided credentials.
136
+ * Depending on backend configuration, may auto-login after registration.
137
+ *
138
+ * @param params - Registration parameters
139
+ * @returns User info and access token (if auto-login enabled)
140
+ *
141
+ * @example
142
+ * // Register with email
143
+ * const result = await client.auth.register({
144
+ * email: 'user@example.com',
145
+ * password: 'SecurePass123',
146
+ * displayName: 'John Doe'
147
+ * });
148
+ *
149
+ * if (result.success) {
150
+ * console.log('User registered:', result.data.user.uid);
151
+ * }
152
+ *
153
+ * @example
154
+ * // Register with username
155
+ * const result = await client.auth.register({
156
+ * username: 'johndoe',
157
+ * password: 'SecurePass123'
158
+ * });
159
+ *
160
+ * @since 1.0.0
161
+ */
162
+ register(params: RegisterParams): Promise<ClientResult<LoginResponse>>;
163
+
164
+ /**
165
+ * Login with password
166
+ *
167
+ * Authenticates a user with username/email/phone and password.
168
+ * On success, access token is automatically stored and attached to all subsequent requests.
169
+ *
170
+ * @param params - Login credentials
171
+ * @returns User info and access token
172
+ *
173
+ * @example
174
+ * // Login with email
175
+ * const result = await client.auth.login({
176
+ * email: 'user@example.com',
177
+ * password: 'myPassword123'
178
+ * });
179
+ *
180
+ * if (result.success) {
181
+ * console.log('Welcome,', result.data.user.displayName);
182
+ * } else {
183
+ * console.error('Login failed:', result.error.message);
184
+ * }
185
+ *
186
+ * @example
187
+ * // Login with username
188
+ * const result = await client.auth.login({
189
+ * username: 'johndoe',
190
+ * password: 'myPassword123'
191
+ * });
192
+ *
193
+ * @example
194
+ * // Login with phone
195
+ * const result = await client.auth.login({
196
+ * phone: '+1234567890',
197
+ * password: 'myPassword123'
198
+ * });
199
+ *
200
+ * @since 1.0.0
201
+ */
202
+ login(params: LoginParams): Promise<ClientResult<LoginResponse>>;
203
+
204
+ /**
205
+ * Change password
206
+ *
207
+ * Changes the password for the current user.
208
+ * Requires the old password for verification.
209
+ *
210
+ * @param params - Old and new passwords
211
+ * @returns Success status
212
+ *
213
+ * @example
214
+ * const result = await client.auth.changePassword({
215
+ * oldPassword: 'currentPass123',
216
+ * newPassword: 'newSecurePass456'
217
+ * });
218
+ *
219
+ * if (result.success) {
220
+ * console.log('Password changed successfully');
221
+ * }
222
+ *
223
+ * @since 1.0.0
224
+ */
225
+ changePassword(params: ChangePasswordParams): Promise<ClientResult<SuccessResponse>>;
226
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * * User profile operations including:
3
+ * - Get current user information
4
+ * - Update profile fields
5
+ * - Avatar management
6
+ *
7
+ * @module auth/profile
8
+ */
9
+
10
+ import type { ClientResult } from '../common';
11
+ import type { User } from './user';
12
+
13
+ // ==================== Parameters ====================
14
+
15
+ /**
16
+ * Update user profile parameters
17
+ *
18
+ * All fields are optional - only update what you want to change.
19
+ *
20
+ */
21
+ export interface UpdateProfileParams {
22
+ /** New display name */
23
+ displayName?: string;
24
+
25
+ /** New avatar URL */
26
+ avatarUrl?: string;
27
+
28
+ /** New email (may require verification) */
29
+ email?: string;
30
+
31
+ /** New phone (may require verification) */
32
+ phone?: string;
33
+ }
34
+
35
+ // ==================== API ====================
36
+
37
+ /**
38
+ * User Profile Management API
39
+ *
40
+ * Methods for managing user profile information.
41
+ */
42
+ export interface ProfileAPI {
43
+ /**
44
+ * Get current user information
45
+ *
46
+ * Retrieves the profile of the currently authenticated user.
47
+ *
48
+ * @returns Current user info with roles and permissions
49
+ *
50
+ */
51
+ getMe(): Promise<ClientResult<User>>;
52
+
53
+ /**
54
+ * Update user profile
55
+ *
56
+ * Updates one or more fields of the current user's profile.
57
+ * Only the fields provided will be updated.
58
+ *
59
+ * @param params - Fields to update
60
+ * @returns Updated user info
61
+ *
62
+ */
63
+ updateProfile(params: UpdateProfileParams): Promise<ClientResult<User>>;
64
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * * Core user data structures including:
3
+ * - User profile information
4
+ * - Role and permission types
5
+ * - User status enums
6
+ *
7
+ * @module auth/user
8
+ */
9
+
10
+ /**
11
+ * User information with optimized roles and permissions
12
+ *
13
+ * Roles and permissions are returned as simple string arrays for efficient client-side use:
14
+ * - `roles`: ["admin", "user", "manager"]
15
+ * - `permissions`: ["user:read", "user:write", "order:read"]
16
+ *
17
+ */
18
+ export interface User {
19
+ /** Unique user identifier */
20
+ uid: string;
21
+
22
+ /** Username (null if not set) */
23
+ username: string | null;
24
+
25
+ /** Email address (null if not set) */
26
+ email: string | null;
27
+
28
+ /** Phone number (null if not set) */
29
+ phone: string | null;
30
+
31
+ /** Display name shown in UI */
32
+ displayName: string | null;
33
+
34
+ /** Avatar image URL */
35
+ avatarUrl: string | null;
36
+
37
+ /** Whether account is active */
38
+ isActive: boolean;
39
+
40
+ /** Whether email is verified */
41
+ emailVerified: boolean;
42
+
43
+ /** Whether phone is verified */
44
+ phoneVerified: boolean;
45
+
46
+ /** Email verification timestamp (ISO 8601) */
47
+ emailVerifiedAt: string | null;
48
+
49
+ /** Phone verification timestamp (ISO 8601) */
50
+ phoneVerifiedAt: string | null;
51
+
52
+ /**
53
+ * Role codes assigned to user
54
+ *
55
+ * Includes system roles and custom roles:
56
+ * - System roles: "anonymous" (unauthenticated), "member" (default), "admin"
57
+ * - Custom roles: defined in app.settings.yml
58
+ *
59
+ */
60
+ roles: string[];
61
+
62
+ /**
63
+ * Permission names granted to user
64
+ * Format: "resource:action"
65
+ */
66
+ permissions: string[];
67
+
68
+ /** Account creation timestamp (ISO 8601) */
69
+ createdAt: string;
70
+
71
+ /** Last update timestamp (ISO 8601) */
72
+ updatedAt: string;
73
+ }