@0xchain/token 1.1.0-beta.35 → 1.1.0-beta.36

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/dist/index.d.ts CHANGED
@@ -8,6 +8,8 @@ import { ITokenManager, ITokenManagerResponse } from './type';
8
8
  */
9
9
  export declare const setToken: (res: ITokenManagerResponse, response?: NextResponse | null) => NextResponse | null;
10
10
  export declare const getToken: (key?: string) => string;
11
+ /** 兼容 OAuth 直出与 { data: token } 包装 */
12
+ export declare function normalizeTokenPayload(res: unknown): ITokenManagerResponse | null;
11
13
  export declare const removeAllToken: () => void;
12
14
  export declare const isTokenExpired: () => Promise<boolean>;
13
15
  declare const TokenManager: ITokenManager;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,QAAQ,CAAC;AA+C9D;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,KAAK,qBAAqB,EAAE,WAAW,YAAY,GAAG,IAAI,KAAG,YAAY,GAAG,IAyCpG,CAAA;AAED,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,WAEpC,CAAA;AAED,eAAO,MAAM,cAAc,YAM1B,CAAA;AAID,eAAO,MAAM,cAAc,wBAiC1B,CAAA;AACD,QAAA,MAAM,YAAY,EAAE,aAMnB,CAAA;AACD,eAAe,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,QAAQ,CAAC;AA+C9D;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,KAAK,qBAAqB,EAAE,WAAW,YAAY,GAAG,IAAI,KAAG,YAAY,GAAG,IAyCpG,CAAA;AAED,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,WAEpC,CAAA;AAED,sCAAsC;AACtC,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,OAAO,GACX,qBAAqB,GAAG,IAAI,CAc9B;AAED,eAAO,MAAM,cAAc,YAM1B,CAAA;AAID,eAAO,MAAM,cAAc,wBA2C1B,CAAA;AACD,QAAA,MAAM,YAAY,EAAE,aAMnB,CAAA;AAED,eAAe,YAAY,CAAA"}
package/dist/index.js CHANGED
@@ -82,6 +82,21 @@ const setToken = (res, response) => {
82
82
  const getToken = (key) => {
83
83
  return Cookies.get(key || "id_token") || "";
84
84
  };
85
+ function normalizeTokenPayload(res) {
86
+ if (!res || typeof res !== "object") return null;
87
+ const payload = res;
88
+ if (typeof payload.access_token === "string") {
89
+ return payload;
90
+ }
91
+ const nested = payload.data;
92
+ if (nested && typeof nested === "object") {
93
+ const data = nested;
94
+ if (typeof data.access_token === "string") {
95
+ return data;
96
+ }
97
+ }
98
+ return null;
99
+ }
85
100
  const removeAllToken = () => {
86
101
  Cookies.remove("id_token", { path: COOKIE_PATH, domain: COOKIE_DOMAIN });
87
102
  Cookies.remove("access_token", { path: COOKIE_PATH, domain: COOKIE_DOMAIN });
@@ -95,12 +110,14 @@ const removeTokenByKey = (key) => {
95
110
  const isTokenExpired = async () => {
96
111
  let t;
97
112
  let refreshToken;
113
+ let accessToken;
98
114
  if (process.env.IS_SERVER) {
99
115
  try {
100
116
  const { cookies } = await import("next/headers");
101
117
  const cookieStore = await cookies();
102
118
  const tokenExpire = cookieStore.get("token_expire")?.value;
103
119
  refreshToken = cookieStore.get("refresh_token")?.value;
120
+ accessToken = cookieStore.get("access_token")?.value;
104
121
  t = tokenExpire ? Number(tokenExpire) : void 0;
105
122
  } catch {
106
123
  return false;
@@ -109,14 +126,22 @@ const isTokenExpired = async () => {
109
126
  const rawExpire = Cookies.get("token_expire");
110
127
  t = rawExpire ? Number(rawExpire) : void 0;
111
128
  refreshToken = Cookies.get("refresh_token");
129
+ accessToken = Cookies.get("access_token");
112
130
  }
113
- if (refreshToken && (!t || Number.isNaN(t))) {
114
- return true;
115
- } else if (t && t - Date.now() < 5 * 60 * 1e3) {
116
- return true;
117
- } else {
131
+ const nearExpiry = (expireAt) => expireAt - Date.now() < 5 * 60 * 1e3;
132
+ if (accessToken) {
133
+ if (t && !Number.isNaN(t)) {
134
+ return nearExpiry(t);
135
+ }
118
136
  return false;
119
137
  }
138
+ if (refreshToken) {
139
+ return true;
140
+ }
141
+ if (t && !Number.isNaN(t)) {
142
+ return nearExpiry(t);
143
+ }
144
+ return false;
120
145
  };
121
146
  const TokenManager = {
122
147
  setToken,
@@ -129,6 +154,7 @@ export {
129
154
  TokenManager as default,
130
155
  getToken,
131
156
  isTokenExpired,
157
+ normalizeTokenPayload,
132
158
  removeAllToken,
133
159
  setToken
134
160
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xchain/token",
3
- "version": "1.1.0-beta.35",
3
+ "version": "1.1.0-beta.36",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -18,13 +18,13 @@
18
18
  "!**/*.tsbuildinfo"
19
19
  ],
20
20
  "peerDependencies": {
21
- "js-cookie": "3.0.5",
22
- "next": "16.1.6"
21
+ "js-cookie": "catalog:",
22
+ "next": "catalog:"
23
23
  },
24
24
  "devDependencies": {
25
- "@types/js-cookie": "3.0.6",
26
- "js-cookie": "3.0.5",
27
- "next": "16.1.6"
25
+ "@types/js-cookie": "catalog:",
26
+ "js-cookie": "catalog:",
27
+ "next": "catalog:"
28
28
  },
29
29
  "nx": {
30
30
  "tags": [
@@ -34,4 +34,4 @@
34
34
  "publishConfig": {
35
35
  "access": "public"
36
36
  }
37
- }
37
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024-present 0xchain
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.