@pubflow/core 0.4.1 → 0.4.2

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,55 @@
1
+ /**
2
+ * Two-Factor Authentication Service
3
+ *
4
+ * Thin client over the multi-flowless /auth/two_factor/* endpoints.
5
+ * All network calls delegate to ApiClient so session cookies are
6
+ * forwarded automatically.
7
+ */
8
+ import { ApiClient } from '../api/client';
9
+ import { PubflowInstanceConfig } from '../types';
10
+ import type { TwoFactorMethod, TwoFactorSystemInfo, TwoFactorStartResult, TwoFactorVerifyResult, TwoFactorSetupResult, TwoFactorToggleResult } from './types';
11
+ export declare class TwoFactorService {
12
+ private apiClient;
13
+ private basePath;
14
+ constructor(apiClient: ApiClient, config: PubflowInstanceConfig);
15
+ /**
16
+ * GET /auth/two_factor/system
17
+ * Returns system-level 2FA availability (no auth needed).
18
+ */
19
+ getSystem(): Promise<TwoFactorSystemInfo>;
20
+ /**
21
+ * GET /auth/two_factor/methods
22
+ * Returns the authenticated user's configured 2FA methods.
23
+ */
24
+ getMethods(): Promise<TwoFactorMethod[]>;
25
+ /**
26
+ * POST /auth/two_factor/:method/setup
27
+ * Begin setup for a 2FA method (email or sms).
28
+ */
29
+ setup(method: string, identifier: string): Promise<TwoFactorSetupResult>;
30
+ /**
31
+ * POST /auth/two_factor/:method/start
32
+ * (Re-)send a verification code for an existing method.
33
+ * Accepts BOTH active and pending sessions (used during login flow).
34
+ */
35
+ start(methodId: string, method: string, action?: string): Promise<TwoFactorStartResult>;
36
+ /**
37
+ * POST /auth/two_factor/verify
38
+ * Verify a 2FA code. For action='login' the pending session becomes active.
39
+ */
40
+ verify(methodId: string, code: string, action?: string): Promise<TwoFactorVerifyResult>;
41
+ /**
42
+ * POST /auth/two_factor/toggle
43
+ * Enable or disable 2FA for the current user.
44
+ */
45
+ toggle(enabled: boolean, verificationCode?: string, verificationMethodId?: string): Promise<TwoFactorToggleResult>;
46
+ /**
47
+ * DELETE /auth/two_factor/:id
48
+ * Remove a 2FA method. Requires verification via another active method.
49
+ */
50
+ removeMethod(methodId: string, verificationCode: string, verificationMethodId: string): Promise<{
51
+ success: boolean;
52
+ message?: string;
53
+ error?: string;
54
+ }>;
55
+ }
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ /**
3
+ * Two-Factor Authentication Service
4
+ *
5
+ * Thin client over the multi-flowless /auth/two_factor/* endpoints.
6
+ * All network calls delegate to ApiClient so session cookies are
7
+ * forwarded automatically.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.TwoFactorService = void 0;
11
+ class TwoFactorService {
12
+ constructor(apiClient, config) {
13
+ this.apiClient = apiClient;
14
+ this.basePath = `${config.authBasePath || '/auth'}/two_factor`;
15
+ }
16
+ // ─── Public (no auth) ──────────────────────────────────────────────────────
17
+ /**
18
+ * GET /auth/two_factor/system
19
+ * Returns system-level 2FA availability (no auth needed).
20
+ */
21
+ async getSystem() {
22
+ const res = await this.apiClient.get(`${this.basePath}/system`, { includeSession: false });
23
+ if (res.success && res.data)
24
+ return res.data;
25
+ return { global_two_factor_enabled: false, available_methods: [] };
26
+ }
27
+ // ─── User methods ──────────────────────────────────────────────────────────
28
+ /**
29
+ * GET /auth/two_factor/methods
30
+ * Returns the authenticated user's configured 2FA methods.
31
+ */
32
+ async getMethods() {
33
+ var _a;
34
+ const res = await this.apiClient.get(`${this.basePath}/methods`);
35
+ if (res.success && res.data)
36
+ return (_a = res.data.methods) !== null && _a !== void 0 ? _a : [];
37
+ return [];
38
+ }
39
+ // ─── Setup ────────────────────────────────────────────────────────────────
40
+ /**
41
+ * POST /auth/two_factor/:method/setup
42
+ * Begin setup for a 2FA method (email or sms).
43
+ */
44
+ async setup(method, identifier) {
45
+ const res = await this.apiClient.post(`${this.basePath}/${method}/setup`, { identifier });
46
+ if (res.success && res.data)
47
+ return res.data;
48
+ return { success: false, error: res.error || 'Setup failed' };
49
+ }
50
+ // ─── Start / Re-send ───────────────────────────────────────────────────────
51
+ /**
52
+ * POST /auth/two_factor/:method/start
53
+ * (Re-)send a verification code for an existing method.
54
+ * Accepts BOTH active and pending sessions (used during login flow).
55
+ */
56
+ async start(methodId, method, action = 'login') {
57
+ const res = await this.apiClient.post(`${this.basePath}/${method}/start`, { method_id: methodId, action });
58
+ if (res.success && res.data)
59
+ return res.data;
60
+ return { success: false, error: res.error || 'Failed to send code' };
61
+ }
62
+ // ─── Verify ───────────────────────────────────────────────────────────────
63
+ /**
64
+ * POST /auth/two_factor/verify
65
+ * Verify a 2FA code. For action='login' the pending session becomes active.
66
+ */
67
+ async verify(methodId, code, action = 'login') {
68
+ const res = await this.apiClient.post(`${this.basePath}/verify`, { method_id: methodId, code, action });
69
+ if (res.success && res.data)
70
+ return res.data;
71
+ // The server may return 400 with body even on wrong code — surface it
72
+ return {
73
+ success: false,
74
+ verified: false,
75
+ error: res.error || 'Verification failed',
76
+ };
77
+ }
78
+ // ─── Toggle ───────────────────────────────────────────────────────────────
79
+ /**
80
+ * POST /auth/two_factor/toggle
81
+ * Enable or disable 2FA for the current user.
82
+ */
83
+ async toggle(enabled, verificationCode, verificationMethodId) {
84
+ const res = await this.apiClient.post(`${this.basePath}/toggle`, {
85
+ two_factor_enabled: enabled,
86
+ ...(verificationCode && { verification_code: verificationCode }),
87
+ ...(verificationMethodId && { verification_method_id: verificationMethodId }),
88
+ });
89
+ if (res.success && res.data)
90
+ return res.data;
91
+ return { success: false, error: res.error || 'Toggle failed' };
92
+ }
93
+ // ─── Remove ───────────────────────────────────────────────────────────────
94
+ /**
95
+ * DELETE /auth/two_factor/:id
96
+ * Remove a 2FA method. Requires verification via another active method.
97
+ */
98
+ async removeMethod(methodId, verificationCode, verificationMethodId) {
99
+ const res = await this.apiClient.request(`${this.basePath}/${methodId}`, 'DELETE', {
100
+ verification_code: verificationCode,
101
+ verification_method_id: verificationMethodId,
102
+ });
103
+ if (res.success && res.data)
104
+ return res.data;
105
+ return { success: false, error: res.error || 'Remove failed' };
106
+ }
107
+ }
108
+ exports.TwoFactorService = TwoFactorService;
@@ -30,7 +30,7 @@ export interface LoginResult {
30
30
  */
31
31
  success: boolean;
32
32
  /**
33
- * User data (only present if success is true)
33
+ * User data (only present if success is true and 2FA is not required)
34
34
  */
35
35
  user?: User;
36
36
  /**
@@ -41,6 +41,16 @@ export interface LoginResult {
41
41
  * Error message (only present if success is false)
42
42
  */
43
43
  error?: string;
44
+ /**
45
+ * Whether 2FA verification is required to complete login.
46
+ * When true, the session is in "pending" status and must be
47
+ * activated via POST /auth/two_factor/verify.
48
+ */
49
+ requires2fa?: boolean;
50
+ /**
51
+ * Available 2FA methods the user has configured (present when requires2fa is true)
52
+ */
53
+ availableMethods?: TwoFactorMethod[];
44
54
  }
45
55
  /**
46
56
  * Result of a session validation
@@ -248,3 +258,67 @@ export interface RefreshResponse {
248
258
  */
249
259
  error?: string;
250
260
  }
261
+ /**
262
+ * A 2FA method configured by the user
263
+ */
264
+ export interface TwoFactorMethod {
265
+ id: string;
266
+ method: 'email' | 'sms' | 'totp' | string;
267
+ status: 'pending_setup' | 'active' | string;
268
+ identifier?: string | null;
269
+ last_used_at?: string | null;
270
+ created_at?: string;
271
+ }
272
+ /**
273
+ * System-level 2FA information (public endpoint)
274
+ */
275
+ export interface TwoFactorSystemInfo {
276
+ global_two_factor_enabled: boolean;
277
+ available_methods: string[];
278
+ }
279
+ /**
280
+ * Result of starting a 2FA verification (sending a code)
281
+ */
282
+ export interface TwoFactorStartResult {
283
+ success: boolean;
284
+ method?: string;
285
+ verification_sent?: boolean;
286
+ expires_in?: number;
287
+ message?: string;
288
+ error?: string;
289
+ }
290
+ /**
291
+ * Result of verifying a 2FA code
292
+ */
293
+ export interface TwoFactorVerifyResult {
294
+ success: boolean;
295
+ verified?: boolean;
296
+ session_activated?: boolean;
297
+ expires_at?: string | null;
298
+ attempts_remaining?: number;
299
+ message?: string;
300
+ error?: string;
301
+ }
302
+ /**
303
+ * Result of setting up a new 2FA method
304
+ */
305
+ export interface TwoFactorSetupResult {
306
+ success: boolean;
307
+ method_id?: string;
308
+ method?: string;
309
+ status?: string;
310
+ verification_sent?: boolean;
311
+ expires_in?: number;
312
+ message?: string;
313
+ error?: string;
314
+ }
315
+ /**
316
+ * Result of toggling 2FA for the user
317
+ */
318
+ export interface TwoFactorToggleResult {
319
+ success: boolean;
320
+ two_factor_enabled?: boolean;
321
+ active_methods?: string[];
322
+ message?: string;
323
+ error?: string;
324
+ }
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  export * from './api/client';
7
7
  export * from './api/types';
8
8
  export * from './auth/service';
9
+ export { TwoFactorService } from './auth/two-factor';
9
10
  export * from './bridge/service';
10
11
  export * from './bridge/types';
11
12
  export * from './config';
@@ -13,7 +14,7 @@ export * from './schema/validator';
13
14
  export * from './storage/adapter';
14
15
  export type { User, SessionConfig, StorageConfig, PubflowInstanceConfig, PaginationMeta } from './types';
15
16
  export type { PubflowInstanceConfig as PubflowConfig } from './types';
16
- export type { LoginCredentials, LoginResult, SessionValidationResult, SessionRefreshResult, RegistrationData, RegistrationResult, PasswordResetRequestData, PasswordResetData, AuthResponse, SessionResponse, ValidationResponse, RefreshResponse } from './auth/types';
17
+ export type { LoginCredentials, LoginResult, SessionValidationResult, SessionRefreshResult, RegistrationData, RegistrationResult, PasswordResetRequestData, PasswordResetData, AuthResponse, SessionResponse, ValidationResponse, RefreshResponse, TwoFactorMethod, TwoFactorSystemInfo, TwoFactorStartResult, TwoFactorVerifyResult, TwoFactorSetupResult, TwoFactorToggleResult, } from './auth/types';
17
18
  export { ApiClient } from './api/client';
18
19
  export { AuthService } from './auth/service';
19
20
  export { StorageAdapter } from './storage/adapter';
package/dist/index.js CHANGED
@@ -19,12 +19,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
19
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.BridgePaymentClient = exports.AuthService = exports.ApiClient = void 0;
22
+ exports.BridgePaymentClient = exports.AuthService = exports.ApiClient = exports.TwoFactorService = void 0;
23
23
  // Export API
24
24
  __exportStar(require("./api/client"), exports);
25
25
  __exportStar(require("./api/types"), exports);
26
26
  // Export Auth
27
27
  __exportStar(require("./auth/service"), exports);
28
+ var two_factor_1 = require("./auth/two-factor");
29
+ Object.defineProperty(exports, "TwoFactorService", { enumerable: true, get: function () { return two_factor_1.TwoFactorService; } });
28
30
  // Export Bridge
29
31
  __exportStar(require("./bridge/service"), exports);
30
32
  __exportStar(require("./bridge/types"), exports);
@@ -153,7 +153,7 @@ export interface UpdatePaymentMethodRequest {
153
153
  /**
154
154
  * Address type
155
155
  */
156
- export type AddressType = 'billing' | 'shipping';
156
+ export type AddressType = 'billing' | 'shipping' | 'both';
157
157
  /**
158
158
  * Address response
159
159
  */
@@ -171,6 +171,7 @@ export interface Address {
171
171
  postal_code: string;
172
172
  country: string;
173
173
  phone?: string;
174
+ email?: string;
174
175
  is_default: boolean;
175
176
  created_at: string;
176
177
  updated_at: string;
@@ -188,6 +189,7 @@ export interface CreateAddressRequest {
188
189
  postal_code: string;
189
190
  country: string;
190
191
  phone?: string;
192
+ email?: string;
191
193
  is_default?: boolean;
192
194
  }
193
195
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pubflow/core",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Core functionality for Pubflow framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",