@0xchain/token 1.1.0-beta.34 → 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 +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -5
- package/package.json +1 -1
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;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
};
|