@communecter/cocolight-api-client 1.0.5 → 1.0.6

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.
package/src/ApiClient.js CHANGED
@@ -5,6 +5,7 @@ import addFormats from "ajv-formats";
5
5
  import axios from "axios";
6
6
  import axiosRetry from "axios-retry";
7
7
  import EJSON from "ejson";
8
+ import { jwtDecode } from "jwt-decode";
8
9
  import pino from "pino";
9
10
 
10
11
  import MongoID from "./EJSONType.js";
@@ -146,6 +147,12 @@ export default class ApiClient extends EventEmitter {
146
147
  setToken(token) {
147
148
  this._accessToken = token;
148
149
  this._client.defaults.headers.common["Authorization"] = "Bearer " + token;
150
+ // Extrait l'id depuis le token et le stocke si disponible
151
+ const userId = this._getIdFromToken(token);
152
+ if (userId) {
153
+ this._setUserId(userId);
154
+ this._logger.debug(`[ApiClient] userId extrait et défini : ${userId}`);
155
+ }
149
156
  this._logger.debug(`[ApiClient] setToken: ${token}`);
150
157
  }
151
158
 
@@ -165,6 +172,12 @@ export default class ApiClient extends EventEmitter {
165
172
  */
166
173
  setRefreshToken(rt) {
167
174
  this._refreshToken = rt;
175
+ // Vous pouvez faire de même ici si besoin
176
+ const userId = this._getIdFromToken(rt);
177
+ if (userId) {
178
+ this._setUserId(userId);
179
+ this._logger.debug(`[ApiClient] userId extrait depuis refreshToken : ${userId}`);
180
+ }
168
181
  this._logger.debug(`[ApiClient] setRefreshToken: ${rt}`);
169
182
  }
170
183
 
@@ -177,6 +190,26 @@ export default class ApiClient extends EventEmitter {
177
190
  return this._refreshToken;
178
191
  }
179
192
 
193
+ /**
194
+ * Extrait l'identifiant depuis un JWT.
195
+ *
196
+ * @param {string} token - Le token JWT (accessToken ou refreshToken).
197
+ * @returns {string|null} L'identifiant extrait ou null si non trouvé.
198
+ */
199
+ _getIdFromToken(token) {
200
+ if (!token) return null;
201
+ try {
202
+ // Décodage du token grâce à jwt-decode
203
+ const payload = jwtDecode(token);
204
+ // L'identifiant peut être dans "id" ou "userId"
205
+ this._logger.log("[ApiClient] Payload décodé :", payload);
206
+ return payload.id || payload.userId || null;
207
+ } catch (err) {
208
+ this._logger.error("[ApiClient] Erreur lors du décodage du token :", err.message);
209
+ return null;
210
+ }
211
+ }
212
+
180
213
  /**
181
214
  * Méthode simplifiée de refresh (en JSON).
182
215
  * Emet un event refreshSuccess si ça marche
@@ -578,9 +611,9 @@ export default class ApiClient extends EventEmitter {
578
611
  this.setRefreshToken(value);
579
612
  break;
580
613
 
581
- case "setUserId":
582
- this._setUserId(value);
583
- break;
614
+ // case "setUserId":
615
+ // this._setUserId(value);
616
+ // break;
584
617
 
585
618
  case "resetSession":
586
619
  this.resetSession();