@goat-bravos/shared-lib-client 1.0.3 → 1.0.4

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.
@@ -1,9 +1,55 @@
1
1
  import { HttpInterceptorFn } from "@angular/common/http";
2
+ /**
3
+ * Cấu hình cho auth interceptor.
4
+ */
5
+ export interface AuthInterceptorConfig {
6
+ /**
7
+ * Danh sách các path pattern sẽ KHÔNG gắn Bearer token.
8
+ * Mỗi phần tử là một chuỗi mà interceptor sẽ kiểm tra bằng `url.includes(pattern)`.
9
+ *
10
+ * Ví dụ: ['/login', '/password-reset', '/public/products']
11
+ */
12
+ excludedPaths?: string[];
13
+ }
14
+ /**
15
+ * Cấu hình auth interceptor từ bên ngoài (thường gọi ở Shell App).
16
+ *
17
+ * @example
18
+ * // Trong app.config.ts của Shell App:
19
+ * import { configureAuthInterceptor } from 'shared-lib-client';
20
+ *
21
+ * configureAuthInterceptor({
22
+ * excludedPaths: [
23
+ * '/login',
24
+ * '/password-reset',
25
+ * '/refresh',
26
+ * '/public/products',
27
+ * '/public/categories',
28
+ * ],
29
+ * });
30
+ */
31
+ export declare function configureAuthInterceptor(config: AuthInterceptorConfig): void;
32
+ /**
33
+ * Thêm các path vào danh sách loại trừ mà không ghi đè danh sách mặc định.
34
+ *
35
+ * @example
36
+ * import { addExcludedPaths } from 'shared-lib-client';
37
+ *
38
+ * addExcludedPaths(['/public/products', '/public/categories']);
39
+ */
40
+ export declare function addExcludedPaths(paths: string[]): void;
41
+ /**
42
+ * Lấy danh sách các path đang bị loại trừ (dùng cho debug/testing).
43
+ */
44
+ export declare function getExcludedPaths(): string[];
2
45
  /**
3
46
  * Auth interceptor dùng chung cho các micro-frontend.
4
47
  * - Tự gắn header Authorization nếu có access token
5
48
  * - Khi gặp lỗi 401 thì phát event để Shell/Auth xử lý refresh token
6
49
  * - Các request đang chờ sẽ được đồng bộ qua `notifyTokenRefreshed`
50
+ *
51
+ * Để cấu hình danh sách path không gắn Bearer, gọi `configureAuthInterceptor()`
52
+ * hoặc `addExcludedPaths()` trước khi ứng dụng bắt đầu gửi request.
7
53
  */
8
54
  export declare const authInterceptor: HttpInterceptorFn;
9
55
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"auth.interceptor.d.ts","sourceRoot":"","sources":["../../src/interceptors/auth.interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,iBAAiB,EAClB,MAAM,sBAAsB,CAAC;AAY9B;;;;;GAKG;AACH,eAAO,MAAM,eAAe,EAAE,iBAqC7B,CAAC;AAiDF;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAG3D"}
1
+ {"version":3,"file":"auth.interceptor.d.ts","sourceRoot":"","sources":["../../src/interceptors/auth.interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,iBAAiB,EAClB,MAAM,sBAAsB,CAAC;AAY9B;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B;AAiBD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,qBAAqB,GAAG,IAAI,CAI5E;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,CAGtD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,EAAE,CAE3C;AASD;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,EAAE,iBAiC7B,CAAC;AAiDF;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAG3D"}
@@ -5,20 +5,79 @@ import { ErrorCode } from "../enums/error-code.enum";
5
5
  // Cờ và subject dùng để đồng bộ các request khi access token hết hạn.
6
6
  let isRefreshing = false;
7
7
  const refreshTokenSubject = new BehaviorSubject(null);
8
+ /**
9
+ * Danh sách path mặc định sẽ không gắn Bearer token.
10
+ */
11
+ const DEFAULT_EXCLUDED_PATHS = [
12
+ "/login",
13
+ "/hrm/users/register",
14
+ "/password-reset",
15
+ "/refresh",
16
+ ];
17
+ /**
18
+ * Danh sách path đang được sử dụng (có thể được cấu hình từ bên ngoài).
19
+ */
20
+ let activeExcludedPaths = [...DEFAULT_EXCLUDED_PATHS];
21
+ /**
22
+ * Cấu hình auth interceptor từ bên ngoài (thường gọi ở Shell App).
23
+ *
24
+ * @example
25
+ * // Trong app.config.ts của Shell App:
26
+ * import { configureAuthInterceptor } from 'shared-lib-client';
27
+ *
28
+ * configureAuthInterceptor({
29
+ * excludedPaths: [
30
+ * '/login',
31
+ * '/password-reset',
32
+ * '/refresh',
33
+ * '/public/products',
34
+ * '/public/categories',
35
+ * ],
36
+ * });
37
+ */
38
+ export function configureAuthInterceptor(config) {
39
+ if (config.excludedPaths && config.excludedPaths.length > 0) {
40
+ activeExcludedPaths = [...config.excludedPaths];
41
+ }
42
+ }
43
+ /**
44
+ * Thêm các path vào danh sách loại trừ mà không ghi đè danh sách mặc định.
45
+ *
46
+ * @example
47
+ * import { addExcludedPaths } from 'shared-lib-client';
48
+ *
49
+ * addExcludedPaths(['/public/products', '/public/categories']);
50
+ */
51
+ export function addExcludedPaths(paths) {
52
+ const newPaths = paths.filter((p) => !activeExcludedPaths.includes(p));
53
+ activeExcludedPaths = [...activeExcludedPaths, ...newPaths];
54
+ }
55
+ /**
56
+ * Lấy danh sách các path đang bị loại trừ (dùng cho debug/testing).
57
+ */
58
+ export function getExcludedPaths() {
59
+ return [...activeExcludedPaths];
60
+ }
61
+ /**
62
+ * Kiểm tra một URL có thuộc danh sách loại trừ hay không.
63
+ */
64
+ function isExcluded(url) {
65
+ return activeExcludedPaths.some((pattern) => url.includes(pattern));
66
+ }
8
67
  /**
9
68
  * Auth interceptor dùng chung cho các micro-frontend.
10
69
  * - Tự gắn header Authorization nếu có access token
11
70
  * - Khi gặp lỗi 401 thì phát event để Shell/Auth xử lý refresh token
12
71
  * - Các request đang chờ sẽ được đồng bộ qua `notifyTokenRefreshed`
72
+ *
73
+ * Để cấu hình danh sách path không gắn Bearer, gọi `configureAuthInterceptor()`
74
+ * hoặc `addExcludedPaths()` trước khi ứng dụng bắt đầu gửi request.
13
75
  */
14
76
  export const authInterceptor = (req, next) => {
15
77
  // 1. Lấy access token từ localStorage
16
78
  const token = StorageUtil.getAccessToken();
17
79
  let authReq = req;
18
- const isExcludedRequest = req.url.includes("/login") ||
19
- req.url.includes("/hrm/users/register") ||
20
- req.url.includes("/password-reset") ||
21
- req.url.includes("/refresh");
80
+ const isExcludedRequest = isExcluded(req.url);
22
81
  // 2. Gắn Authorization header nếu request không nằm trong nhóm loại trừ
23
82
  if (token && !isExcludedRequest) {
24
83
  authReq = req.clone({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goat-bravos/shared-lib-client",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "type": "module",
5
5
  "peerDependencies": {
6
6
  "@angular/common": "21.0.1",