@zerosls/clm-sdk 2.1.6 → 2.1.7

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.
@@ -47,6 +47,70 @@ export class AuthApi {
47
47
 
48
48
  return { ok: true };
49
49
  }*/
50
+ /*async login(
51
+ credentials: LoginCredentials,
52
+ mode: LoginMode = "new"
53
+ ): Promise<{ ok: boolean }> {
54
+ const path = mode === "legacy" ? "/legacy/login" : "/auth/login";
55
+
56
+ console.log(`🔐 Login attempt (${mode}):`, credentials.email);
57
+
58
+ const response = await fetch(`${this.apiClient["baseUrl"]}${path}`, {
59
+ method: "POST",
60
+ headers: {
61
+ "Content-Type": "application/json",
62
+ "X-Organization": this.apiClient["organization"] || "default-org",
63
+ },
64
+ credentials: "include",
65
+ body: JSON.stringify({
66
+ email: credentials.email,
67
+ password: credentials.password,
68
+ }),
69
+ });
70
+
71
+ if (!response.ok) {
72
+ if (response.status === 401) throw new Error("Invalid credentials");
73
+ throw new Error(`Login failed: ${response.status}`);
74
+ }
75
+
76
+ const data: any = await response.json().catch(() => null);
77
+
78
+ if (mode === "legacy") {
79
+ // ✅ Token LEGACY (del sistema viejo)
80
+ const legacyToken = data?.dataResult?.token || data?.token;
81
+
82
+ if (legacyToken) {
83
+ if (typeof sessionStorage !== "undefined") {
84
+ sessionStorage.setItem("legacy_token", legacyToken);
85
+ }
86
+ if (typeof window !== "undefined") {
87
+ (window as any).__LEGACY_TOKEN__ = legacyToken;
88
+ }
89
+ console.log("✅ Legacy token saved");
90
+ } else {
91
+ console.warn("⚠️ No legacy token received");
92
+ }
93
+ } else {
94
+ // ✅ Token NUEVO (del sistema moderno)
95
+ const newToken = data?.token;
96
+
97
+ if (newToken) {
98
+ if (typeof sessionStorage !== "undefined") {
99
+ sessionStorage.setItem("auth_token", newToken);
100
+ }
101
+ if (typeof window !== "undefined") {
102
+ (window as any).__AUTH_TOKEN__ = newToken;
103
+ }
104
+ console.log("✅ Auth token saved");
105
+ } else {
106
+ console.warn("⚠️ No auth token received");
107
+ }
108
+ }
109
+
110
+ this.apiClient.setToken(null);
111
+
112
+ return { ok: true };
113
+ }*/
50
114
  async login(credentials, mode = "new") {
51
115
  var _a;
52
116
  const path = mode === "legacy" ? "/legacy/login" : "/auth/login";
@@ -55,7 +119,7 @@ export class AuthApi {
55
119
  method: "POST",
56
120
  headers: {
57
121
  "Content-Type": "application/json",
58
- "X-Organization": this.apiClient["organization"] || "default-org",
122
+ "X-Organization": this.apiClient["organization"] || "qa",
59
123
  },
60
124
  credentials: "include",
61
125
  body: JSON.stringify({
@@ -64,14 +128,29 @@ export class AuthApi {
64
128
  }),
65
129
  });
66
130
  if (!response.ok) {
131
+ console.error(`❌ Login ${mode} failed:`, response.status);
67
132
  if (response.status === 401)
68
133
  throw new Error("Invalid credentials");
69
134
  throw new Error(`Login failed: ${response.status}`);
70
135
  }
71
- const data = await response.json().catch(() => null);
136
+ // Lee el JSON con manejo de errores
137
+ let data;
138
+ try {
139
+ data = await response.json();
140
+ console.log(`📦 Response data (${mode}):`, data); // ✅ Agrega esto
141
+ }
142
+ catch (error) {
143
+ console.error("❌ Error parsing JSON:", error);
144
+ return { ok: false };
145
+ }
72
146
  if (mode === "legacy") {
73
- // ✅ Token LEGACY (del sistema viejo)
74
147
  const legacyToken = ((_a = data === null || data === void 0 ? void 0 : data.dataResult) === null || _a === void 0 ? void 0 : _a.token) || (data === null || data === void 0 ? void 0 : data.token);
148
+ console.log("🔍 Legacy token check:", {
149
+ hasData: !!data,
150
+ hasDataResult: !!(data === null || data === void 0 ? void 0 : data.dataResult),
151
+ hasToken: !!legacyToken,
152
+ tokenLength: legacyToken === null || legacyToken === void 0 ? void 0 : legacyToken.length,
153
+ });
75
154
  if (legacyToken) {
76
155
  if (typeof sessionStorage !== "undefined") {
77
156
  sessionStorage.setItem("legacy_token", legacyToken);
@@ -79,15 +158,20 @@ export class AuthApi {
79
158
  if (typeof window !== "undefined") {
80
159
  window.__LEGACY_TOKEN__ = legacyToken;
81
160
  }
82
- console.log("✅ Legacy token saved");
161
+ console.log("✅ Legacy token saved:", legacyToken.substring(0, 20) + "...");
83
162
  }
84
163
  else {
85
- console.warn("⚠️ No legacy token received");
164
+ console.warn("⚠️ No legacy token in response:", data);
86
165
  }
87
166
  }
88
167
  else {
89
- // ✅ Token NUEVO (del sistema moderno)
90
168
  const newToken = data === null || data === void 0 ? void 0 : data.token;
169
+ console.log("🔍 Auth token check:", {
170
+ hasData: !!data,
171
+ hasToken: !!newToken,
172
+ tokenLength: newToken === null || newToken === void 0 ? void 0 : newToken.length,
173
+ dataKeys: Object.keys(data || {}),
174
+ });
91
175
  if (newToken) {
92
176
  if (typeof sessionStorage !== "undefined") {
93
177
  sessionStorage.setItem("auth_token", newToken);
@@ -95,10 +179,10 @@ export class AuthApi {
95
179
  if (typeof window !== "undefined") {
96
180
  window.__AUTH_TOKEN__ = newToken;
97
181
  }
98
- console.log("✅ Auth token saved");
182
+ console.log("✅ Auth token saved:", newToken.substring(0, 20) + "...");
99
183
  }
100
184
  else {
101
- console.warn("⚠️ No auth token received");
185
+ console.warn("⚠️ No auth token in response. Data:", data);
102
186
  }
103
187
  }
104
188
  this.apiClient.setToken(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerosls/clm-sdk",
3
- "version": "2.1.6",
3
+ "version": "2.1.7",
4
4
  "description": "SDK for ZeroCLM API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -56,7 +56,7 @@ export class AuthApi {
56
56
  return { ok: true };
57
57
  }*/
58
58
 
59
- async login(
59
+ /*async login(
60
60
  credentials: LoginCredentials,
61
61
  mode: LoginMode = "new"
62
62
  ): Promise<{ ok: boolean }> {
@@ -118,6 +118,93 @@ export class AuthApi {
118
118
 
119
119
  this.apiClient.setToken(null);
120
120
 
121
+ return { ok: true };
122
+ }*/
123
+ async login(
124
+ credentials: LoginCredentials,
125
+ mode: LoginMode = "new"
126
+ ): Promise<{ ok: boolean }> {
127
+ const path = mode === "legacy" ? "/legacy/login" : "/auth/login";
128
+
129
+ console.log(`🔐 Login attempt (${mode}):`, credentials.email);
130
+
131
+ const response = await fetch(`${this.apiClient["baseUrl"]}${path}`, {
132
+ method: "POST",
133
+ headers: {
134
+ "Content-Type": "application/json",
135
+ "X-Organization": this.apiClient["organization"] || "qa",
136
+ },
137
+ credentials: "include",
138
+ body: JSON.stringify({
139
+ email: credentials.email,
140
+ password: credentials.password,
141
+ }),
142
+ });
143
+
144
+ if (!response.ok) {
145
+ console.error(`❌ Login ${mode} failed:`, response.status);
146
+ if (response.status === 401) throw new Error("Invalid credentials");
147
+ throw new Error(`Login failed: ${response.status}`);
148
+ }
149
+
150
+ // ✅ Lee el JSON con manejo de errores
151
+ let data: any;
152
+ try {
153
+ data = await response.json();
154
+ console.log(`📦 Response data (${mode}):`, data); // ✅ Agrega esto
155
+ } catch (error) {
156
+ console.error("❌ Error parsing JSON:", error);
157
+ return { ok: false };
158
+ }
159
+
160
+ if (mode === "legacy") {
161
+ const legacyToken = data?.dataResult?.token || data?.token;
162
+
163
+ console.log("🔍 Legacy token check:", {
164
+ hasData: !!data,
165
+ hasDataResult: !!data?.dataResult,
166
+ hasToken: !!legacyToken,
167
+ tokenLength: legacyToken?.length,
168
+ });
169
+
170
+ if (legacyToken) {
171
+ if (typeof sessionStorage !== "undefined") {
172
+ sessionStorage.setItem("legacy_token", legacyToken);
173
+ }
174
+ if (typeof window !== "undefined") {
175
+ (window as any).__LEGACY_TOKEN__ = legacyToken;
176
+ }
177
+ console.log(
178
+ "✅ Legacy token saved:",
179
+ legacyToken.substring(0, 20) + "..."
180
+ );
181
+ } else {
182
+ console.warn("⚠️ No legacy token in response:", data);
183
+ }
184
+ } else {
185
+ const newToken = data?.token;
186
+
187
+ console.log("🔍 Auth token check:", {
188
+ hasData: !!data,
189
+ hasToken: !!newToken,
190
+ tokenLength: newToken?.length,
191
+ dataKeys: Object.keys(data || {}),
192
+ });
193
+
194
+ if (newToken) {
195
+ if (typeof sessionStorage !== "undefined") {
196
+ sessionStorage.setItem("auth_token", newToken);
197
+ }
198
+ if (typeof window !== "undefined") {
199
+ (window as any).__AUTH_TOKEN__ = newToken;
200
+ }
201
+ console.log("✅ Auth token saved:", newToken.substring(0, 20) + "...");
202
+ } else {
203
+ console.warn("⚠️ No auth token in response. Data:", data);
204
+ }
205
+ }
206
+
207
+ this.apiClient.setToken(null);
121
208
  return { ok: true };
122
209
  }
123
210