@0xchain/token 1.1.0-beta.33 → 1.1.0-beta.35
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.map +1 -1
- package/dist/index.js +14 -7
- package/package.json +1 -1
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;
|
|
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"}
|
package/dist/index.js
CHANGED
|
@@ -3,30 +3,36 @@ const COOKIE_DOMAIN = ".ichatgo.ai";
|
|
|
3
3
|
const COOKIE_PATH = "/";
|
|
4
4
|
const COOKIE_SAME_SITE = "lax";
|
|
5
5
|
const COOKIE_SECURE = process.env.NODE_ENV === "production";
|
|
6
|
+
const REFRESH_TOKEN_COOKIE_DAYS = 7;
|
|
7
|
+
function expiresFromSeconds(seconds) {
|
|
8
|
+
return new Date(Date.now() + Math.max(seconds, 0) * 1e3);
|
|
9
|
+
}
|
|
6
10
|
const cookieSetToken = (data) => {
|
|
11
|
+
const expiresIn = data?.expires_in ?? 3600;
|
|
12
|
+
const accessExpires = expiresFromSeconds(expiresIn);
|
|
7
13
|
Cookies.set("access_token", data?.access_token, {
|
|
8
|
-
expires:
|
|
14
|
+
expires: accessExpires,
|
|
9
15
|
domain: COOKIE_DOMAIN,
|
|
10
16
|
sameSite: COOKIE_SAME_SITE,
|
|
11
17
|
secure: COOKIE_SECURE,
|
|
12
18
|
path: COOKIE_PATH
|
|
13
19
|
});
|
|
14
20
|
Cookies.set("refresh_token", data?.refresh_token, {
|
|
15
|
-
expires:
|
|
21
|
+
expires: REFRESH_TOKEN_COOKIE_DAYS,
|
|
16
22
|
domain: COOKIE_DOMAIN,
|
|
17
23
|
sameSite: COOKIE_SAME_SITE,
|
|
18
24
|
secure: COOKIE_SECURE,
|
|
19
25
|
path: COOKIE_PATH
|
|
20
26
|
});
|
|
21
27
|
Cookies.set("id_token", data?.id_token, {
|
|
22
|
-
expires:
|
|
28
|
+
expires: accessExpires,
|
|
23
29
|
domain: COOKIE_DOMAIN,
|
|
24
30
|
sameSite: COOKIE_SAME_SITE,
|
|
25
31
|
secure: COOKIE_SECURE,
|
|
26
32
|
path: COOKIE_PATH
|
|
27
33
|
});
|
|
28
|
-
Cookies.set("token_expire", (Date.now() +
|
|
29
|
-
expires:
|
|
34
|
+
Cookies.set("token_expire", (Date.now() + expiresIn * 1e3).toString(), {
|
|
35
|
+
expires: accessExpires,
|
|
30
36
|
domain: COOKIE_DOMAIN,
|
|
31
37
|
sameSite: COOKIE_SAME_SITE,
|
|
32
38
|
secure: COOKIE_SECURE,
|
|
@@ -100,10 +106,11 @@ const isTokenExpired = async () => {
|
|
|
100
106
|
return false;
|
|
101
107
|
}
|
|
102
108
|
} else {
|
|
103
|
-
|
|
109
|
+
const rawExpire = Cookies.get("token_expire");
|
|
110
|
+
t = rawExpire ? Number(rawExpire) : void 0;
|
|
104
111
|
refreshToken = Cookies.get("refresh_token");
|
|
105
112
|
}
|
|
106
|
-
if (!t
|
|
113
|
+
if (refreshToken && (!t || Number.isNaN(t))) {
|
|
107
114
|
return true;
|
|
108
115
|
} else if (t && t - Date.now() < 5 * 60 * 1e3) {
|
|
109
116
|
return true;
|