@zerosls/clm-sdk 2.1.6 → 2.1.8
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.
|
@@ -8,6 +8,8 @@ export class AuthApi {
|
|
|
8
8
|
): Promise<{ ok: boolean }> {
|
|
9
9
|
const path = mode === "legacy" ? "/legacy/login" : "/auth/login";
|
|
10
10
|
|
|
11
|
+
console.log(`🔐 Login attempt (${mode}):`, credentials.email);
|
|
12
|
+
|
|
11
13
|
const response = await fetch(`${this.apiClient["baseUrl"]}${path}`, {
|
|
12
14
|
method: "POST",
|
|
13
15
|
headers: {
|
|
@@ -28,8 +30,8 @@ export class AuthApi {
|
|
|
28
30
|
|
|
29
31
|
const data: any = await response.json().catch(() => null);
|
|
30
32
|
|
|
31
|
-
// ✅ Si es legacy, guarda Bearer token para siguientes llamadas legacy
|
|
32
33
|
if (mode === "legacy") {
|
|
34
|
+
// ✅ Token LEGACY (del sistema viejo)
|
|
33
35
|
const legacyToken = data?.dataResult?.token || data?.token;
|
|
34
36
|
|
|
35
37
|
if (legacyToken) {
|
|
@@ -39,10 +41,27 @@ export class AuthApi {
|
|
|
39
41
|
if (typeof window !== "undefined") {
|
|
40
42
|
(window as any).__LEGACY_TOKEN__ = legacyToken;
|
|
41
43
|
}
|
|
44
|
+
console.log("✅ Legacy token saved");
|
|
45
|
+
} else {
|
|
46
|
+
console.warn("⚠️ No legacy token received");
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
// ✅ Token NUEVO (del sistema moderno)
|
|
50
|
+
const newToken = data?.token;
|
|
51
|
+
|
|
52
|
+
if (newToken) {
|
|
53
|
+
if (typeof sessionStorage !== "undefined") {
|
|
54
|
+
sessionStorage.setItem("auth_token", newToken);
|
|
55
|
+
}
|
|
56
|
+
if (typeof window !== "undefined") {
|
|
57
|
+
(window as any).__AUTH_TOKEN__ = newToken;
|
|
58
|
+
}
|
|
59
|
+
console.log("✅ Auth token saved");
|
|
60
|
+
} else {
|
|
61
|
+
console.warn("⚠️ No auth token received");
|
|
42
62
|
}
|
|
43
63
|
}
|
|
44
64
|
|
|
45
|
-
// ✅ Nuevo: cookie HttpOnly, no guardes token en memoria del SDK
|
|
46
65
|
this.apiClient.setToken(null);
|
|
47
66
|
|
|
48
67
|
return { ok: true };
|
|
@@ -55,7 +74,7 @@ export class AuthApi {
|
|
|
55
74
|
method: "POST",
|
|
56
75
|
headers: {
|
|
57
76
|
"Content-Type": "application/json",
|
|
58
|
-
"X-Organization": this.apiClient["organization"] || "
|
|
77
|
+
"X-Organization": this.apiClient["organization"] || "qa",
|
|
59
78
|
},
|
|
60
79
|
credentials: "include",
|
|
61
80
|
body: JSON.stringify({
|
|
@@ -64,14 +83,29 @@ export class AuthApi {
|
|
|
64
83
|
}),
|
|
65
84
|
});
|
|
66
85
|
if (!response.ok) {
|
|
86
|
+
console.error(`❌ Login ${mode} failed:`, response.status);
|
|
67
87
|
if (response.status === 401)
|
|
68
88
|
throw new Error("Invalid credentials");
|
|
69
89
|
throw new Error(`Login failed: ${response.status}`);
|
|
70
90
|
}
|
|
71
|
-
|
|
91
|
+
// ✅ Lee el JSON con manejo de errores
|
|
92
|
+
let data;
|
|
93
|
+
try {
|
|
94
|
+
data = await response.json();
|
|
95
|
+
console.log(`📦 Response data (${mode}):`, data); // ✅ Agrega esto
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error("❌ Error parsing JSON:", error);
|
|
99
|
+
return { ok: false };
|
|
100
|
+
}
|
|
72
101
|
if (mode === "legacy") {
|
|
73
|
-
// ✅ Token LEGACY (del sistema viejo)
|
|
74
102
|
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);
|
|
103
|
+
console.log("🔍 Legacy token check:", {
|
|
104
|
+
hasData: !!data,
|
|
105
|
+
hasDataResult: !!(data === null || data === void 0 ? void 0 : data.dataResult),
|
|
106
|
+
hasToken: !!legacyToken,
|
|
107
|
+
tokenLength: legacyToken === null || legacyToken === void 0 ? void 0 : legacyToken.length,
|
|
108
|
+
});
|
|
75
109
|
if (legacyToken) {
|
|
76
110
|
if (typeof sessionStorage !== "undefined") {
|
|
77
111
|
sessionStorage.setItem("legacy_token", legacyToken);
|
|
@@ -79,15 +113,20 @@ export class AuthApi {
|
|
|
79
113
|
if (typeof window !== "undefined") {
|
|
80
114
|
window.__LEGACY_TOKEN__ = legacyToken;
|
|
81
115
|
}
|
|
82
|
-
console.log("✅ Legacy token saved");
|
|
116
|
+
console.log("✅ Legacy token saved:", legacyToken.substring(0, 20) + "...");
|
|
83
117
|
}
|
|
84
118
|
else {
|
|
85
|
-
console.warn("⚠️ No legacy token
|
|
119
|
+
console.warn("⚠️ No legacy token in response:", data);
|
|
86
120
|
}
|
|
87
121
|
}
|
|
88
122
|
else {
|
|
89
|
-
// ✅ Token NUEVO (del sistema moderno)
|
|
90
123
|
const newToken = data === null || data === void 0 ? void 0 : data.token;
|
|
124
|
+
console.log("🔍 Auth token check:", {
|
|
125
|
+
hasData: !!data,
|
|
126
|
+
hasToken: !!newToken,
|
|
127
|
+
tokenLength: newToken === null || newToken === void 0 ? void 0 : newToken.length,
|
|
128
|
+
dataKeys: Object.keys(data || {}),
|
|
129
|
+
});
|
|
91
130
|
if (newToken) {
|
|
92
131
|
if (typeof sessionStorage !== "undefined") {
|
|
93
132
|
sessionStorage.setItem("auth_token", newToken);
|
|
@@ -95,10 +134,10 @@ export class AuthApi {
|
|
|
95
134
|
if (typeof window !== "undefined") {
|
|
96
135
|
window.__AUTH_TOKEN__ = newToken;
|
|
97
136
|
}
|
|
98
|
-
console.log("✅ Auth token saved");
|
|
137
|
+
console.log("✅ Auth token saved:", newToken.substring(0, 20) + "...");
|
|
99
138
|
}
|
|
100
139
|
else {
|
|
101
|
-
console.warn("⚠️ No auth token
|
|
140
|
+
console.warn("⚠️ No auth token in response. Data:", data);
|
|
102
141
|
}
|
|
103
142
|
}
|
|
104
143
|
this.apiClient.setToken(null);
|
|
@@ -110,22 +149,6 @@ export class AuthApi {
|
|
|
110
149
|
this.apiClient.setToken(response.token);
|
|
111
150
|
return response;
|
|
112
151
|
}
|
|
113
|
-
/*async logout(): Promise<void> {
|
|
114
|
-
try {
|
|
115
|
-
await this.apiClient.post<void>("/auth/logout");
|
|
116
|
-
} finally {
|
|
117
|
-
// limpia token nuevo
|
|
118
|
-
this.apiClient.setToken(null);
|
|
119
|
-
|
|
120
|
-
// limpia legacy token
|
|
121
|
-
if (typeof sessionStorage !== "undefined") {
|
|
122
|
-
sessionStorage.removeItem("legacy_token");
|
|
123
|
-
}
|
|
124
|
-
if (typeof window !== "undefined") {
|
|
125
|
-
delete (window as any).__LEGACY_TOKEN__;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}*/
|
|
129
152
|
async logout() {
|
|
130
153
|
try {
|
|
131
154
|
await this.apiClient.post("/auth/logout");
|
|
@@ -147,15 +170,6 @@ export class AuthApi {
|
|
|
147
170
|
}
|
|
148
171
|
}
|
|
149
172
|
}
|
|
150
|
-
//isAuthenticated(): boolean {
|
|
151
|
-
// const hasToken = this.apiClient.getToken() !== null;
|
|
152
|
-
// const hasLegacy =
|
|
153
|
-
// (typeof sessionStorage !== "undefined" &&
|
|
154
|
-
// !!sessionStorage.getItem("legacy_token")) ||
|
|
155
|
-
// (typeof window !== "undefined" && !!(window as any).__LEGACY_TOKEN__);
|
|
156
|
-
//
|
|
157
|
-
// return hasToken || hasLegacy;
|
|
158
|
-
//}
|
|
159
173
|
setToken(token) {
|
|
160
174
|
this.apiClient.setToken(token);
|
|
161
175
|
}
|
package/package.json
CHANGED
|
@@ -16,6 +16,8 @@ export class AuthApi {
|
|
|
16
16
|
): Promise<{ ok: boolean }> {
|
|
17
17
|
const path = mode === "legacy" ? "/legacy/login" : "/auth/login";
|
|
18
18
|
|
|
19
|
+
console.log(`🔐 Login attempt (${mode}):`, credentials.email);
|
|
20
|
+
|
|
19
21
|
const response = await fetch(`${this.apiClient["baseUrl"]}${path}`, {
|
|
20
22
|
method: "POST",
|
|
21
23
|
headers: {
|
|
@@ -36,8 +38,8 @@ export class AuthApi {
|
|
|
36
38
|
|
|
37
39
|
const data: any = await response.json().catch(() => null);
|
|
38
40
|
|
|
39
|
-
// ✅ Si es legacy, guarda Bearer token para siguientes llamadas legacy
|
|
40
41
|
if (mode === "legacy") {
|
|
42
|
+
// ✅ Token LEGACY (del sistema viejo)
|
|
41
43
|
const legacyToken = data?.dataResult?.token || data?.token;
|
|
42
44
|
|
|
43
45
|
if (legacyToken) {
|
|
@@ -47,15 +49,31 @@ export class AuthApi {
|
|
|
47
49
|
if (typeof window !== "undefined") {
|
|
48
50
|
(window as any).__LEGACY_TOKEN__ = legacyToken;
|
|
49
51
|
}
|
|
52
|
+
console.log("✅ Legacy token saved");
|
|
53
|
+
} else {
|
|
54
|
+
console.warn("⚠️ No legacy token received");
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
// ✅ Token NUEVO (del sistema moderno)
|
|
58
|
+
const newToken = data?.token;
|
|
59
|
+
|
|
60
|
+
if (newToken) {
|
|
61
|
+
if (typeof sessionStorage !== "undefined") {
|
|
62
|
+
sessionStorage.setItem("auth_token", newToken);
|
|
63
|
+
}
|
|
64
|
+
if (typeof window !== "undefined") {
|
|
65
|
+
(window as any).__AUTH_TOKEN__ = newToken;
|
|
66
|
+
}
|
|
67
|
+
console.log("✅ Auth token saved");
|
|
68
|
+
} else {
|
|
69
|
+
console.warn("⚠️ No auth token received");
|
|
50
70
|
}
|
|
51
71
|
}
|
|
52
72
|
|
|
53
|
-
// ✅ Nuevo: cookie HttpOnly, no guardes token en memoria del SDK
|
|
54
73
|
this.apiClient.setToken(null);
|
|
55
74
|
|
|
56
75
|
return { ok: true };
|
|
57
76
|
}*/
|
|
58
|
-
|
|
59
77
|
async login(
|
|
60
78
|
credentials: LoginCredentials,
|
|
61
79
|
mode: LoginMode = "new"
|
|
@@ -68,7 +86,7 @@ export class AuthApi {
|
|
|
68
86
|
method: "POST",
|
|
69
87
|
headers: {
|
|
70
88
|
"Content-Type": "application/json",
|
|
71
|
-
"X-Organization": this.apiClient["organization"] || "
|
|
89
|
+
"X-Organization": this.apiClient["organization"] || "qa",
|
|
72
90
|
},
|
|
73
91
|
credentials: "include",
|
|
74
92
|
body: JSON.stringify({
|
|
@@ -78,16 +96,31 @@ export class AuthApi {
|
|
|
78
96
|
});
|
|
79
97
|
|
|
80
98
|
if (!response.ok) {
|
|
99
|
+
console.error(`❌ Login ${mode} failed:`, response.status);
|
|
81
100
|
if (response.status === 401) throw new Error("Invalid credentials");
|
|
82
101
|
throw new Error(`Login failed: ${response.status}`);
|
|
83
102
|
}
|
|
84
103
|
|
|
85
|
-
|
|
104
|
+
// ✅ Lee el JSON con manejo de errores
|
|
105
|
+
let data: any;
|
|
106
|
+
try {
|
|
107
|
+
data = await response.json();
|
|
108
|
+
console.log(`📦 Response data (${mode}):`, data); // ✅ Agrega esto
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error("❌ Error parsing JSON:", error);
|
|
111
|
+
return { ok: false };
|
|
112
|
+
}
|
|
86
113
|
|
|
87
114
|
if (mode === "legacy") {
|
|
88
|
-
// ✅ Token LEGACY (del sistema viejo)
|
|
89
115
|
const legacyToken = data?.dataResult?.token || data?.token;
|
|
90
116
|
|
|
117
|
+
console.log("🔍 Legacy token check:", {
|
|
118
|
+
hasData: !!data,
|
|
119
|
+
hasDataResult: !!data?.dataResult,
|
|
120
|
+
hasToken: !!legacyToken,
|
|
121
|
+
tokenLength: legacyToken?.length,
|
|
122
|
+
});
|
|
123
|
+
|
|
91
124
|
if (legacyToken) {
|
|
92
125
|
if (typeof sessionStorage !== "undefined") {
|
|
93
126
|
sessionStorage.setItem("legacy_token", legacyToken);
|
|
@@ -95,14 +128,23 @@ export class AuthApi {
|
|
|
95
128
|
if (typeof window !== "undefined") {
|
|
96
129
|
(window as any).__LEGACY_TOKEN__ = legacyToken;
|
|
97
130
|
}
|
|
98
|
-
console.log(
|
|
131
|
+
console.log(
|
|
132
|
+
"✅ Legacy token saved:",
|
|
133
|
+
legacyToken.substring(0, 20) + "..."
|
|
134
|
+
);
|
|
99
135
|
} else {
|
|
100
|
-
console.warn("⚠️ No legacy token
|
|
136
|
+
console.warn("⚠️ No legacy token in response:", data);
|
|
101
137
|
}
|
|
102
138
|
} else {
|
|
103
|
-
// ✅ Token NUEVO (del sistema moderno)
|
|
104
139
|
const newToken = data?.token;
|
|
105
140
|
|
|
141
|
+
console.log("🔍 Auth token check:", {
|
|
142
|
+
hasData: !!data,
|
|
143
|
+
hasToken: !!newToken,
|
|
144
|
+
tokenLength: newToken?.length,
|
|
145
|
+
dataKeys: Object.keys(data || {}),
|
|
146
|
+
});
|
|
147
|
+
|
|
106
148
|
if (newToken) {
|
|
107
149
|
if (typeof sessionStorage !== "undefined") {
|
|
108
150
|
sessionStorage.setItem("auth_token", newToken);
|
|
@@ -110,14 +152,13 @@ export class AuthApi {
|
|
|
110
152
|
if (typeof window !== "undefined") {
|
|
111
153
|
(window as any).__AUTH_TOKEN__ = newToken;
|
|
112
154
|
}
|
|
113
|
-
console.log("✅ Auth token saved");
|
|
155
|
+
console.log("✅ Auth token saved:", newToken.substring(0, 20) + "...");
|
|
114
156
|
} else {
|
|
115
|
-
console.warn("⚠️ No auth token
|
|
157
|
+
console.warn("⚠️ No auth token in response. Data:", data);
|
|
116
158
|
}
|
|
117
159
|
}
|
|
118
160
|
|
|
119
161
|
this.apiClient.setToken(null);
|
|
120
|
-
|
|
121
162
|
return { ok: true };
|
|
122
163
|
}
|
|
123
164
|
|
|
@@ -131,23 +172,6 @@ export class AuthApi {
|
|
|
131
172
|
return response;
|
|
132
173
|
}
|
|
133
174
|
|
|
134
|
-
/*async logout(): Promise<void> {
|
|
135
|
-
try {
|
|
136
|
-
await this.apiClient.post<void>("/auth/logout");
|
|
137
|
-
} finally {
|
|
138
|
-
// limpia token nuevo
|
|
139
|
-
this.apiClient.setToken(null);
|
|
140
|
-
|
|
141
|
-
// limpia legacy token
|
|
142
|
-
if (typeof sessionStorage !== "undefined") {
|
|
143
|
-
sessionStorage.removeItem("legacy_token");
|
|
144
|
-
}
|
|
145
|
-
if (typeof window !== "undefined") {
|
|
146
|
-
delete (window as any).__LEGACY_TOKEN__;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}*/
|
|
150
|
-
|
|
151
175
|
async logout(): Promise<void> {
|
|
152
176
|
try {
|
|
153
177
|
await this.apiClient.post<void>("/auth/logout");
|
|
@@ -170,16 +194,6 @@ export class AuthApi {
|
|
|
170
194
|
}
|
|
171
195
|
}
|
|
172
196
|
|
|
173
|
-
//isAuthenticated(): boolean {
|
|
174
|
-
// const hasToken = this.apiClient.getToken() !== null;
|
|
175
|
-
// const hasLegacy =
|
|
176
|
-
// (typeof sessionStorage !== "undefined" &&
|
|
177
|
-
// !!sessionStorage.getItem("legacy_token")) ||
|
|
178
|
-
// (typeof window !== "undefined" && !!(window as any).__LEGACY_TOKEN__);
|
|
179
|
-
//
|
|
180
|
-
// return hasToken || hasLegacy;
|
|
181
|
-
//}
|
|
182
|
-
|
|
183
197
|
setToken(token: string | null): void {
|
|
184
198
|
this.apiClient.setToken(token);
|
|
185
199
|
}
|