@ph-cms/client-sdk 0.1.24 → 0.1.26

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/README.md CHANGED
@@ -1302,6 +1302,18 @@ client.content // ContentModule
1302
1302
  client.channel // ChannelModule
1303
1303
  client.terms // TermsModule
1304
1304
  client.media // MediaModule
1305
+
1306
+ await client.getToken() // 현재 유효한 PH-CMS access token 반환 (만료 시 자동 갱신, 없으면 null)
1307
+ ```
1308
+
1309
+ `getToken()`은 PH-CMS SDK가 아닌 외부 서비스(예: 알림 서비스)를 클라이언트에서 직접 호출할 때 Authorization 헤더에 넣을 토큰을 가져오는 데 사용합니다.
1310
+
1311
+ ```ts
1312
+ const token = await client.getToken();
1313
+
1314
+ const res = await fetch(`${NOTIFICATION_SERVICE_URL}/notifications`, {
1315
+ headers: { Authorization: `Bearer ${token}` },
1316
+ });
1305
1317
  ```
1306
1318
 
1307
1319
  `apiPrefix`는 생성 시 각 모듈에 직접 주입됩니다. 예를 들어 `apiPrefix: '/v2/api'`로 설정하면 모든 모듈의 요청 URL이 `/v2/api/contents/...`, `/v2/api/auth/...` 형태로 전송됩니다.
@@ -30,15 +30,13 @@ export declare class FirebaseAuthProvider extends BaseAuthProvider {
30
30
  expiryBufferMs?: number;
31
31
  });
32
32
  /**
33
- * Returns a valid access token for PH-CMS API calls.
33
+ * Returns the current valid PH-CMS access token.
34
34
  *
35
35
  * 1. If a PH-CMS access token exists and is still valid, return it.
36
36
  * 2. If the PH-CMS access token is expired (or will expire within
37
37
  * `expiryBufferMs`), attempt an automatic refresh using the stored
38
38
  * refresh token.
39
- * 3. If no PH-CMS token exists at all (or refresh failed), fall back to
40
- * the Firebase ID token (useful for the initial exchange call or if the
41
- * server supports Firebase tokens directly).
39
+ * 3. If no PH-CMS token exists or refresh fails, return `null`.
42
40
  */
43
41
  getToken(): Promise<string | null>;
44
42
  /**
@@ -28,32 +28,22 @@ class FirebaseAuthProvider extends base_provider_1.BaseAuthProvider {
28
28
  this.type = 'FIREBASE';
29
29
  }
30
30
  /**
31
- * Returns a valid access token for PH-CMS API calls.
31
+ * Returns the current valid PH-CMS access token.
32
32
  *
33
33
  * 1. If a PH-CMS access token exists and is still valid, return it.
34
34
  * 2. If the PH-CMS access token is expired (or will expire within
35
35
  * `expiryBufferMs`), attempt an automatic refresh using the stored
36
36
  * refresh token.
37
- * 3. If no PH-CMS token exists at all (or refresh failed), fall back to
38
- * the Firebase ID token (useful for the initial exchange call or if the
39
- * server supports Firebase tokens directly).
37
+ * 3. If no PH-CMS token exists or refresh fails, return `null`.
40
38
  */
41
39
  async getToken() {
42
- if (this.accessToken) {
43
- const expired = this.isCurrentTokenExpired();
44
- if (expired === true) {
45
- const refreshed = await this.tryRefresh();
46
- if (refreshed)
47
- return refreshed;
48
- // Refresh failed — fall through to Firebase ID token fallback.
49
- }
50
- else {
51
- // Token is valid (or unparseable — let server decide).
52
- return this.accessToken;
53
- }
40
+ if (!this.accessToken)
41
+ return null;
42
+ const expired = this.isCurrentTokenExpired();
43
+ if (expired === true) {
44
+ return this.tryRefresh();
54
45
  }
55
- // Fallback to Firebase ID token if no valid PH-CMS token is available.
56
- return this.getIdToken();
46
+ return this.accessToken;
57
47
  }
58
48
  /**
59
49
  * Clears PH-CMS tokens **and** signs the user out of Firebase.
package/dist/client.d.ts CHANGED
@@ -35,6 +35,15 @@ export declare class PHCMSClient {
35
35
  private refreshQueue;
36
36
  /** Exposes the auth provider so UI layers can check token state synchronously. */
37
37
  get authProvider(): AuthProvider | undefined;
38
+ /**
39
+ * Returns the current valid PH-CMS access token.
40
+ *
41
+ * Delegates to the configured {@link AuthProvider.getToken}, which handles
42
+ * expiry checks and automatic refresh internally.
43
+ *
44
+ * Returns `null` when no provider is configured or no valid token exists.
45
+ */
46
+ getToken(): Promise<string | null>;
38
47
  private readonly normalizedApiPrefix;
39
48
  constructor(config: PHCMSClientConfig);
40
49
  /**
package/dist/client.js CHANGED
@@ -17,6 +17,17 @@ class PHCMSClient {
17
17
  get authProvider() {
18
18
  return this.config.auth;
19
19
  }
20
+ /**
21
+ * Returns the current valid PH-CMS access token.
22
+ *
23
+ * Delegates to the configured {@link AuthProvider.getToken}, which handles
24
+ * expiry checks and automatic refresh internally.
25
+ *
26
+ * Returns `null` when no provider is configured or no valid token exists.
27
+ */
28
+ async getToken() {
29
+ return this.config.auth?.getToken() ?? null;
30
+ }
20
31
  constructor(config) {
21
32
  this.config = config;
22
33
  /**
package/dist/context.d.ts CHANGED
@@ -22,6 +22,8 @@ export interface PHCMSContextType {
22
22
  hasToken: boolean;
23
23
  /** Manually trigger a refetch of the current user profile. */
24
24
  refreshUser: () => Promise<void>;
25
+ /** Returns the current valid PH-CMS access token (만료 시 자동 갱신, 없으면 null). */
26
+ getToken: () => Promise<string | null>;
25
27
  }
26
28
  export interface PHCMSProviderProps {
27
29
  client: PHCMSClient;
package/dist/context.js CHANGED
@@ -98,6 +98,7 @@ const PHCMSInternalProvider = ({ client, channelUid, children }) => {
98
98
  hasToken,
99
99
  channelUid,
100
100
  refreshUser,
101
+ getToken: () => client.getToken(),
101
102
  };
102
103
  }, [client, user, isError, isLoading, hasToken, channelUid, refreshUser]);
103
104
  return react_1.default.createElement(PHCMSContext.Provider, { value: value }, children);
@@ -240,6 +240,7 @@ export declare const useAuth: () => {
240
240
  username?: string;
241
241
  }, unknown>;
242
242
  logout: import("@tanstack/react-query").UseMutateAsyncFunction<void, Error, void, unknown>;
243
+ getToken: () => Promise<string | null>;
243
244
  loginStatus: import("@tanstack/react-query").UseMutationResult<{
244
245
  refreshToken: string;
245
246
  user: {
@@ -9,7 +9,7 @@ const AUTH_ME_QUERY_KEY = ['auth', 'me'];
9
9
  * Returns both the current auth status and the actions.
10
10
  */
11
11
  const useAuth = () => {
12
- const { user, isAuthenticated, isLoading, refreshUser, channelUid } = (0, context_1.usePHCMSContext)();
12
+ const { user, isAuthenticated, isLoading, refreshUser, channelUid, getToken } = (0, context_1.usePHCMSContext)();
13
13
  const client = (0, context_1.usePHCMS)();
14
14
  const queryClient = (0, react_query_1.useQueryClient)();
15
15
  const loginMutation = (0, react_query_1.useMutation)({
@@ -78,6 +78,7 @@ const useAuth = () => {
78
78
  loginAnonymous: loginAnonymousMutation.mutateAsync,
79
79
  upgradeAnonymous: upgradeAnonymousMutation.mutateAsync,
80
80
  logout: logoutMutation.mutateAsync,
81
+ getToken,
81
82
  // Access to mutation objects for loading/error states if needed
82
83
  loginStatus: loginMutation,
83
84
  loginWithFirebaseStatus: loginWithFirebaseMutation,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ph-cms/client-sdk",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
4
4
  "description": "Unified PH-CMS Client SDK (React + Core)",
5
5
  "keywords": [],
6
6
  "license": "MIT",