@scalemule/nextjs 0.1.50 → 0.1.51

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.
@@ -37,8 +37,9 @@ function validateIP(ip) {
37
37
  function extractClientContext(request) {
38
38
  const headers3 = request.headers;
39
39
  let ip;
40
+ ip = validateIP(headers3.get("x-real-ip"));
40
41
  const cfConnectingIp = headers3.get("cf-connecting-ip");
41
- if (cfConnectingIp) {
42
+ if (!ip && cfConnectingIp) {
42
43
  ip = validateIP(cfConnectingIp);
43
44
  }
44
45
  if (!ip) {
@@ -98,8 +99,9 @@ function extractClientContextFromReq(req) {
98
99
  return value;
99
100
  };
100
101
  let ip;
102
+ ip = validateIP(getHeader("x-real-ip"));
101
103
  const cfConnectingIp = getHeader("cf-connecting-ip");
102
- if (cfConnectingIp) {
104
+ if (!ip && cfConnectingIp) {
103
105
  ip = validateIP(cfConnectingIp);
104
106
  }
105
107
  if (!ip) {
@@ -225,6 +227,12 @@ var ScaleMuleServer = class {
225
227
  /**
226
228
  * Login user - returns session token (store in HTTP-only cookie)
227
229
  */
230
+ sendMfaCode: async (pending_token, method, options) => {
231
+ return this.request("POST", "/v1/auth/mfa/send-code", { body: { pending_token, method }, clientContext: options?.clientContext });
232
+ },
233
+ completeMfa: async (pending_token, code, method, options) => {
234
+ return this.request("POST", "/v1/auth/mfa/verify", { body: { pending_token, code, method }, clientContext: options?.clientContext });
235
+ },
228
236
  login: async (data, options) => {
229
237
  return this.request("POST", "/v1/auth/login", { body: data, clientContext: options?.clientContext });
230
238
  },
@@ -864,7 +872,7 @@ var ScaleMuleServer = class {
864
872
  responseData = text ? JSON.parse(text) : null;
865
873
  } catch {
866
874
  }
867
- if (!response.ok) {
875
+ if (!response.ok || responseData?.success === false) {
868
876
  const baseError = responseData?.error || {
869
877
  code: `HTTP_${response.status}`,
870
878
  message: responseData?.message || text || response.statusText
@@ -1390,21 +1398,46 @@ function createAuthRoutes(config = {}) {
1390
1398
  }
1391
1399
  return registerResponse;
1392
1400
  }
1401
+ case "mfa/send-code": {
1402
+ const { pending_token, method } = body;
1403
+ if (!pending_token || !["email", "sms"].includes(method)) return errorResponse("VALIDATION_ERROR", "MFA token and method required", 400);
1404
+ return successResponse(await sm.auth.sendMfaCode(pending_token, method, { clientContext }));
1405
+ }
1406
+ case "mfa/verify": {
1407
+ const { pending_token, code, method } = body;
1408
+ if (!pending_token || !code || !["totp", "email", "sms", "backup_code"].includes(method)) return errorResponse("VALIDATION_ERROR", "MFA token, code, and valid method required", 400);
1409
+ const result = await sm.auth.completeMfa(pending_token, code, method, { clientContext });
1410
+ if (!result.session_token || !result.user) return errorResponse("MFA_FAILED", "Unable to complete sign-in", 400);
1411
+ await config.onLogin?.({ id: result.user.id, email: result.user.email });
1412
+ const response = withSession(result, { user: result.user, sessionToken: result.session_token, userId: result.user.id }, cookieOptions);
1413
+ if (config.enableAccountSwitcher) {
1414
+ appendKnownAccountCookie(response.headers, {
1415
+ userId: result.user.id,
1416
+ email: result.user.email,
1417
+ fullName: result.user.full_name ?? void 0,
1418
+ avatarUrl: result.user.avatar_url ?? void 0,
1419
+ provider: "email",
1420
+ lastActiveAt: (/* @__PURE__ */ new Date()).toISOString()
1421
+ }, getKnownAccountsCookieRaw(request), cookieOptions, config.accountSwitcherPrivacy);
1422
+ }
1423
+ return response;
1424
+ }
1393
1425
  // ==================== Login ====================
1394
1426
  case "login": {
1395
- const { email, password, remember_me } = body;
1427
+ const { email, password, remember_me, device_fingerprint, challenge_token, challenge_code } = body;
1396
1428
  if (!email || !password) {
1397
1429
  return errorResponse("VALIDATION_ERROR", "Email and password required", 400);
1398
1430
  }
1399
1431
  let loginData;
1400
1432
  try {
1401
- loginData = await sm.auth.login({ email, password, remember_me }, { clientContext });
1433
+ loginData = await sm.auth.login({ email, password, remember_me, device_fingerprint, challenge_token, challenge_code }, { clientContext });
1402
1434
  } catch (err) {
1403
1435
  const apiErr = err instanceof ScaleMuleApiError ? err : null;
1404
1436
  const errorCode = apiErr?.code || "LOGIN_FAILED";
1405
- let status = 400;
1437
+ let status = ["LOGIN_CHALLENGE_REQUIRED", "MFA_REQUIRED"].includes(errorCode) ? 202 : 400;
1438
+ if (errorCode === "CHALLENGE_RATE_LIMITED") status = 429;
1406
1439
  if (errorCode === "INVALID_CREDENTIALS" || errorCode === "UNAUTHORIZED") status = 401;
1407
- if (["EMAIL_NOT_VERIFIED", "PHONE_NOT_VERIFIED", "ACCOUNT_LOCKED", "ACCOUNT_DISABLED", "MFA_REQUIRED"].includes(errorCode)) {
1440
+ if (["EMAIL_NOT_VERIFIED", "PHONE_NOT_VERIFIED", "ACCOUNT_LOCKED", "ACCOUNT_DISABLED"].includes(errorCode)) {
1408
1441
  status = 403;
1409
1442
  }
1410
1443
  return errorResponse(
@@ -1483,7 +1516,7 @@ function createAuthRoutes(config = {}) {
1483
1516
  400
1484
1517
  );
1485
1518
  }
1486
- return successResponse({ message: "Password reset successful" });
1519
+ return clearSession({ message: "Password reset successful" }, cookieOptions);
1487
1520
  }
1488
1521
  // ==================== Verify Email ====================
1489
1522
  case "verify-email": {
@@ -1676,6 +1709,10 @@ function createAuthRoutes(config = {}) {
1676
1709
  return errorResponse("NOT_FOUND", `Unknown endpoint: ${path}`, 404);
1677
1710
  }
1678
1711
  } catch (err) {
1712
+ if (path.startsWith("mfa/") && err instanceof ScaleMuleApiError) {
1713
+ const status = ["CHALLENGE_RATE_LIMITED", "MFA_RATE_LIMITED", "MFA_MAX_ATTEMPTS"].includes(err.code) ? 429 : 400;
1714
+ return errorResponse(err.code, err.message, status);
1715
+ }
1679
1716
  console.error("[ScaleMule Auth] Error:", err);
1680
1717
  return errorResponse("SERVER_ERROR", "Internal server error", 500);
1681
1718
  }
@@ -1,3 +1,3 @@
1
- export { g as createWebhookHandler } from '../webhook-handler-CVmBvPFN.mjs';
1
+ export { g as createWebhookHandler } from '../webhook-handler-DKt6H8FB.mjs';
2
2
  import '@scalemule/money';
3
- import '../index-Bv1m_qQ8.mjs';
3
+ import '../index-BQgmT53x.mjs';
@@ -1,3 +1,3 @@
1
- export { g as createWebhookHandler } from '../webhook-handler-5ymVecTD.js';
1
+ export { g as createWebhookHandler } from '../webhook-handler-44RRJH-r.js';
2
2
  import '@scalemule/money';
3
- import '../index-Bv1m_qQ8.js';
3
+ import '../index-BQgmT53x.js';
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactNode } from 'react';
3
- import { U as User, A as ApiError, a9 as StorageFile } from './index-Bv1m_qQ8.mjs';
3
+ import { U as User, A as ApiError, a9 as StorageFile } from './index-BQgmT53x.mjs';
4
4
 
5
5
  interface MockUserOptions {
6
6
  id?: string;
package/dist/testing.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactNode } from 'react';
3
- import { U as User, A as ApiError, a9 as StorageFile } from './index-Bv1m_qQ8.js';
3
+ import { U as User, A as ApiError, a9 as StorageFile } from './index-BQgmT53x.js';
4
4
 
5
5
  interface MockUserOptions {
6
6
  id?: string;
@@ -1,5 +1,5 @@
1
1
  import { MoneyClient } from '@scalemule/money';
2
- import { A as ApiError, $ as RegisterRequest, p as ClientContext, U as User, v as LoginRequest, L as LoginResponse, c as ListFilesParams, t as ListFilesResponse, a9 as StorageFile, af as UploadResponse } from './index-Bv1m_qQ8.js';
2
+ import { A as ApiError, $ as RegisterRequest, p as ClientContext, U as User, L as LoginResponse, v as LoginRequest, c as ListFilesParams, t as ListFilesResponse, a9 as StorageFile, af as UploadResponse } from './index-BQgmT53x.js';
3
3
 
4
4
  /**
5
5
  * Server-Side ScaleMule Client
@@ -58,6 +58,14 @@ declare class ScaleMuleServer {
58
58
  /**
59
59
  * Login user - returns session token (store in HTTP-only cookie)
60
60
  */
61
+ sendMfaCode: (pending_token: string, method: string, options?: {
62
+ clientContext?: ClientContext;
63
+ }) => Promise<{
64
+ success: boolean;
65
+ }>;
66
+ completeMfa: (pending_token: string, code: string, method: string, options?: {
67
+ clientContext?: ClientContext;
68
+ }) => Promise<LoginResponse>;
61
69
  login: (data: LoginRequest, options?: {
62
70
  clientContext?: ClientContext;
63
71
  }) => Promise<LoginResponse>;
@@ -1,5 +1,5 @@
1
1
  import { MoneyClient } from '@scalemule/money';
2
- import { A as ApiError, $ as RegisterRequest, p as ClientContext, U as User, v as LoginRequest, L as LoginResponse, c as ListFilesParams, t as ListFilesResponse, a9 as StorageFile, af as UploadResponse } from './index-Bv1m_qQ8.mjs';
2
+ import { A as ApiError, $ as RegisterRequest, p as ClientContext, U as User, L as LoginResponse, v as LoginRequest, c as ListFilesParams, t as ListFilesResponse, a9 as StorageFile, af as UploadResponse } from './index-BQgmT53x.mjs';
3
3
 
4
4
  /**
5
5
  * Server-Side ScaleMule Client
@@ -58,6 +58,14 @@ declare class ScaleMuleServer {
58
58
  /**
59
59
  * Login user - returns session token (store in HTTP-only cookie)
60
60
  */
61
+ sendMfaCode: (pending_token: string, method: string, options?: {
62
+ clientContext?: ClientContext;
63
+ }) => Promise<{
64
+ success: boolean;
65
+ }>;
66
+ completeMfa: (pending_token: string, code: string, method: string, options?: {
67
+ clientContext?: ClientContext;
68
+ }) => Promise<LoginResponse>;
61
69
  login: (data: LoginRequest, options?: {
62
70
  clientContext?: ClientContext;
63
71
  }) => Promise<LoginResponse>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalemule/nextjs",
3
- "version": "0.1.50",
3
+ "version": "0.1.51",
4
4
  "description": "ScaleMule SDK for Next.js applications - authentication, storage, and user management",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -68,7 +68,7 @@
68
68
  "dependencies": {
69
69
  "@scalemule/ledvery": "^0.0.1",
70
70
  "@scalemule/money": "^0.0.1",
71
- "@scalemule/sdk": "^0.0.64"
71
+ "@scalemule/sdk": "^0.0.68"
72
72
  },
73
73
  "peerDependencies": {
74
74
  "next": ">=14.0.0",