@arcblock/did-connect-react 3.1.57 → 3.1.58
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/lib/Session/hooks/use-session-token.js +29 -29
- package/lib/Storage/engine/cookie.js +6 -5
- package/lib/Storage/engine/local-storage.js +4 -3
- package/lib/package.json.js +1 -1
- package/package.json +5 -5
- package/src/Session/hooks/use-session-token.js +1 -0
- package/src/Storage/engine/cookie.js +2 -0
- package/src/Storage/engine/local-storage.js +2 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useState as M } from "react";
|
|
2
2
|
import { useCreation as N, useMemoizedFn as c, useInterval as se } from "ahooks";
|
|
3
|
-
import { joinURL as
|
|
3
|
+
import { joinURL as O, withQuery as te } from "ufo";
|
|
4
4
|
import T from "js-cookie";
|
|
5
5
|
import { setVisitorId as ie, getCookieOptions as ce } from "@arcblock/ux/lib/Util";
|
|
6
6
|
import fe from "jwt-decode";
|
|
@@ -10,7 +10,7 @@ import ae from "lodash/noop";
|
|
|
10
10
|
import { ReactGA as de } from "@arcblock/ux/lib/withTracker";
|
|
11
11
|
import { GA_LAST_SOURCE_PROVIDER as ke } from "@arcblock/ux/lib/withTracker/constant";
|
|
12
12
|
import Te from "../../Service/index.js";
|
|
13
|
-
import { createAxios as he, debugTmp as
|
|
13
|
+
import { createAxios as he, debugTmp as C, getBrowserLang as me, logger as b, debug as u, sleep as pe, debugTmpInterval as z, decrypt as Re } from "../../utils.js";
|
|
14
14
|
import { CHECK_INTERVAL_TIME as h, LANG_COOKIE_NAME as ye } from "../../constant.js";
|
|
15
15
|
class m extends Error {
|
|
16
16
|
}
|
|
@@ -26,31 +26,31 @@ function qe({
|
|
|
26
26
|
apiOptions: B = {},
|
|
27
27
|
onRefresh: $ = ae
|
|
28
28
|
}) {
|
|
29
|
-
const [D, y] = M(0), [Q, d] = M(h), { getToken: v, setToken:
|
|
29
|
+
const [D, y] = M(0), [Q, d] = M(h), { getToken: v, setToken: g, removeToken: X } = V, { setToken: Y } = P, { getToken: w, setToken: S, removeToken: J } = q, W = N(() => he({
|
|
30
30
|
baseURL: R,
|
|
31
31
|
timeout: 10 * 1e3,
|
|
32
32
|
secure: !0
|
|
33
33
|
}), [R]), k = () => {
|
|
34
|
-
X(), J();
|
|
35
|
-
},
|
|
34
|
+
C("[removeToken] useSessionToken -> removeToken"), X(), J();
|
|
35
|
+
}, _ = N(() => Te(
|
|
36
36
|
{
|
|
37
37
|
sessionTokenStorage: V,
|
|
38
38
|
refreshTokenStorage: q,
|
|
39
39
|
serviceHost: R,
|
|
40
40
|
authServicePrefix: i.prefix,
|
|
41
41
|
onRefreshTokenError() {
|
|
42
|
-
|
|
42
|
+
C("[removeToken] useSessionToken -> createService: onRefreshTokenError"), k(), e.user && (e.user = null);
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
45
|
B
|
|
46
|
-
), [R, i.prefix]),
|
|
46
|
+
), [R, i.prefix]), I = () => {
|
|
47
47
|
const r = ce({ returnDomain: !1 });
|
|
48
48
|
T.remove("connected_did", r), T.remove("connected_pk", r), T.remove("connected_app", r), T.remove("connected_wallet_os", r), z("[removeToken] clearSession-interval"), k();
|
|
49
49
|
}, Z = c(() => {
|
|
50
50
|
const r = T.get(ye) || me();
|
|
51
51
|
r !== i.currentLocale && (i.currentLocale = r);
|
|
52
52
|
}), H = c(async () => {
|
|
53
|
-
const r = () =>
|
|
53
|
+
const r = () => _.get(O(i.notificationPrefix, "/unread-count"));
|
|
54
54
|
try {
|
|
55
55
|
const { data: n } = await r();
|
|
56
56
|
return n.unReadCount;
|
|
@@ -66,7 +66,7 @@ function qe({
|
|
|
66
66
|
const { data: o, status: f } = await n(), a = o?.user?.did;
|
|
67
67
|
if (a && de.set({
|
|
68
68
|
user_id: a
|
|
69
|
-
}), o?.user?.sourceProvider && localStorage.setItem(ke, o.user.sourceProvider), f === 400 && (
|
|
69
|
+
}), o?.user?.sourceProvider && localStorage.setItem(ke, o.user.sourceProvider), f === 400 && (C("[removeToken] useSessionToken -> _refresh: status 400"), k(), e.user = null, e.error = ""), o.error)
|
|
70
70
|
throw e.error = o.error, e.open = !1, e.unReadCount = 0, new Error(o.error);
|
|
71
71
|
if (o.user) {
|
|
72
72
|
try {
|
|
@@ -75,12 +75,12 @@ function qe({
|
|
|
75
75
|
} catch (l) {
|
|
76
76
|
b.error("getUnreadCount error", l), e.unReadCount = 0;
|
|
77
77
|
}
|
|
78
|
-
o.nextToken && (
|
|
78
|
+
o.nextToken && (g(o.nextToken), $({
|
|
79
79
|
type: s,
|
|
80
80
|
sessionToken: o.nextToken,
|
|
81
81
|
refreshToken: o.nextRefreshToken,
|
|
82
82
|
user: o.user
|
|
83
|
-
}), o.nextRefreshToken &&
|
|
83
|
+
}), o.nextRefreshToken && S(o.nextRefreshToken)), t || (e.open = !1, e.user = o.user, e.provider = o.provider, e.walletOS = o.walletOS), v() || le.error("Can't write session token, please use https url to access this page");
|
|
84
84
|
} else
|
|
85
85
|
throw r && (e.loading = i.autoConnect), e.open = i.autoConnect, e.user = null, e.provider = "", e.walletOS = "", e.unReadCount = 0, new m("Invalid token");
|
|
86
86
|
return { data: o, status: f };
|
|
@@ -90,9 +90,9 @@ function qe({
|
|
|
90
90
|
r && (e.loading = !1);
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
|
-
),
|
|
93
|
+
), x = c(async (r = !1, n = !1) => {
|
|
94
94
|
u("handleRefreshToken", { showProgress: r, onlyRefreshToken: n });
|
|
95
|
-
const t = () => W.post(te(
|
|
95
|
+
const t = () => W.post(te(O(i.prefix, "/refreshSession")), null, {
|
|
96
96
|
headers: {
|
|
97
97
|
authorization: `Bearer ${encodeURIComponent(w())}`
|
|
98
98
|
}
|
|
@@ -101,7 +101,7 @@ function qe({
|
|
|
101
101
|
try {
|
|
102
102
|
await F({ showProgress: r, requestFn: t, onlyRefreshToken: n, type: "refreshToken" }), y(0), d(h);
|
|
103
103
|
} catch (s) {
|
|
104
|
-
s instanceof m ? (console.warn("refresh token failed, remove all tokens"), y(0),
|
|
104
|
+
s instanceof m ? (console.warn("refresh token failed, remove all tokens"), y(0), C("[removeToken] useSessionToken -> handleRefreshToken: refresh token failed, remove all tokens"), k(), d(h)) : (b.error("refresh token failed, unexpected error:", s), y(D + 1), await pe(h), D >= ve ? (y(0), d(void 0)) : d(h));
|
|
105
105
|
}
|
|
106
106
|
}), G = c((r = !1) => {
|
|
107
107
|
if (j)
|
|
@@ -118,9 +118,9 @@ function qe({
|
|
|
118
118
|
}
|
|
119
119
|
else
|
|
120
120
|
t = !0;
|
|
121
|
-
t && (w() ?
|
|
121
|
+
t && (w() ? x(!1, !0) : (z("[clearSession-interval] useSessionToken -> checkToken: empty refreshToken"), I(), e.user && (e.user = null)));
|
|
122
122
|
}), K = c(async ({ showProgress: r = !1, forceRefreshToken: n = !1 } = {}) => {
|
|
123
|
-
const t = () => ue(() =>
|
|
123
|
+
const t = () => ue(() => _.get(O(i.prefix, "/session"), { secure: !0 }), {
|
|
124
124
|
retries: 2
|
|
125
125
|
});
|
|
126
126
|
u("handleRefreshUser", { showProgress: r, forceRefreshToken: n });
|
|
@@ -129,11 +129,11 @@ function qe({
|
|
|
129
129
|
throw new m("need force refresh token");
|
|
130
130
|
await F({ showProgress: r, requestFn: t, type: "refreshUser" });
|
|
131
131
|
} catch (s) {
|
|
132
|
-
s instanceof m && (u("handleRefreshUser failed, try to refresh token", { err: s }), await
|
|
132
|
+
s instanceof m && (u("handleRefreshUser failed, try to refresh token", { err: s }), await x());
|
|
133
133
|
}
|
|
134
134
|
}), ee = c(async () => {
|
|
135
135
|
const r = v(), n = w();
|
|
136
|
-
e.user || (r || n ? await K() : (u("[clearSession] syncSessionSate: empty sessionToken and refreshToken"),
|
|
136
|
+
e.user || (r || n ? await K() : (u("[clearSession] syncSessionSate: empty sessionToken and refreshToken"), I(), e.user && (e.user = null)));
|
|
137
137
|
}), re = c(() => {
|
|
138
138
|
if (e.initialized && e.user)
|
|
139
139
|
try {
|
|
@@ -143,16 +143,16 @@ function qe({
|
|
|
143
143
|
}), oe = c((r) => {
|
|
144
144
|
const n = Array.isArray(r) ? r[0] : r;
|
|
145
145
|
u("handleLoginResult", { loginResult: n, result: r });
|
|
146
|
-
const { loginToken: t, csrfToken: s, sessionToken: o, refreshToken: f, visitorId: a, encrypted: l = !0 } = n,
|
|
147
|
-
let
|
|
148
|
-
if (
|
|
149
|
-
if (
|
|
146
|
+
const { loginToken: t, csrfToken: s, sessionToken: o, refreshToken: f, visitorId: a, encrypted: l = !0 } = n, L = t || o;
|
|
147
|
+
let E, U, A;
|
|
148
|
+
if (L) {
|
|
149
|
+
if (E = l ? p(L) : L, u("handleLoginResult: setSessionToken", { decryptSessionToken: E }), g(E), s) {
|
|
150
150
|
const ne = l ? p(s) : s;
|
|
151
151
|
Y(ne);
|
|
152
152
|
}
|
|
153
|
-
f && (
|
|
153
|
+
f && (U = l ? p(f) : f, u("handleLoginResult: setRefreshToken", { decryptRefreshToken: U }), S(U));
|
|
154
154
|
}
|
|
155
|
-
a && (
|
|
155
|
+
a && (A = l ? p(a) : a, u("handleLoginResult: setVisitorId", { decryptVisitorId: A }), ie(A));
|
|
156
156
|
});
|
|
157
157
|
return se(
|
|
158
158
|
() => {
|
|
@@ -163,17 +163,17 @@ function qe({
|
|
|
163
163
|
), {
|
|
164
164
|
renewToken: re,
|
|
165
165
|
handleRefreshUser: K,
|
|
166
|
-
handleRefreshToken:
|
|
166
|
+
handleRefreshToken: x,
|
|
167
167
|
syncSessionSate: ee,
|
|
168
168
|
handleLoginResult: oe,
|
|
169
169
|
decrypt: p,
|
|
170
170
|
removeToken: k,
|
|
171
|
-
clearSession:
|
|
171
|
+
clearSession: I,
|
|
172
172
|
getSessionToken: v,
|
|
173
173
|
getRefreshToken: w,
|
|
174
|
-
setRefreshToken:
|
|
175
|
-
setSessionToken:
|
|
176
|
-
service:
|
|
174
|
+
setRefreshToken: S,
|
|
175
|
+
setSessionToken: g,
|
|
176
|
+
service: _
|
|
177
177
|
};
|
|
178
178
|
}
|
|
179
179
|
export {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import o from "js-cookie";
|
|
2
2
|
import { getCookieOptions as t } from "@arcblock/ux/lib/Util";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import { debugTmp as s } from "../../utils.js";
|
|
4
|
+
class a {
|
|
5
|
+
constructor(e, r) {
|
|
6
|
+
this.storageKey = e, this.options = r || {};
|
|
6
7
|
}
|
|
7
8
|
setToken(e) {
|
|
8
9
|
return o.set(this.storageKey, e, {
|
|
@@ -13,9 +14,9 @@ class g {
|
|
|
13
14
|
return o.get(this.storageKey);
|
|
14
15
|
}
|
|
15
16
|
removeToken() {
|
|
16
|
-
return o.remove(this.storageKey, t(this.options));
|
|
17
|
+
return s("[CookieStorageEngine] removeToken"), o.remove(this.storageKey, t(this.options));
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
export {
|
|
20
|
-
|
|
21
|
+
a as default
|
|
21
22
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import o from "jwt-decode";
|
|
2
|
-
|
|
2
|
+
import { debugTmp as r } from "../../utils.js";
|
|
3
|
+
class f {
|
|
3
4
|
constructor(e) {
|
|
4
5
|
this.storageKey = e;
|
|
5
6
|
}
|
|
@@ -27,10 +28,10 @@ class a {
|
|
|
27
28
|
return t;
|
|
28
29
|
}
|
|
29
30
|
removeToken() {
|
|
30
|
-
if (!(typeof window > "u" || typeof window.localStorage > "u"))
|
|
31
|
+
if (r("[LocalStorageEngine] removeToken"), !(typeof window > "u" || typeof window.localStorage > "u"))
|
|
31
32
|
return localStorage.removeItem(this.storageKey);
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
export {
|
|
35
|
-
|
|
36
|
+
f as default
|
|
36
37
|
};
|
package/lib/package.json.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcblock/did-connect-react",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.58",
|
|
4
4
|
"description": "Client side library to work with DID Connect by ArcBlock.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
"url": "https://github.com/ArcBlock/ux/issues"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@arcblock/bridge": "3.1.
|
|
35
|
+
"@arcblock/bridge": "3.1.58",
|
|
36
36
|
"@arcblock/did": "^1.27.2",
|
|
37
|
-
"@arcblock/icons": "3.1.
|
|
38
|
-
"@arcblock/react-hooks": "3.1.
|
|
37
|
+
"@arcblock/icons": "3.1.58",
|
|
38
|
+
"@arcblock/react-hooks": "3.1.58",
|
|
39
39
|
"@arcblock/ws": "^1.27.2",
|
|
40
40
|
"@fontsource/lexend": "^5.2.9",
|
|
41
41
|
"@iconify-icons/logos": "^1.2.36",
|
|
@@ -80,5 +80,5 @@
|
|
|
80
80
|
"eslint-plugin-react-hooks": "^4.6.2",
|
|
81
81
|
"jest": "^29.7.0"
|
|
82
82
|
},
|
|
83
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "126e58b5963d5824c397169e26887a09981693e9"
|
|
84
84
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Cookie from 'js-cookie';
|
|
2
2
|
import { getCookieOptions } from '@arcblock/ux/lib/Util';
|
|
3
|
+
import { debugTmp } from '../../utils';
|
|
3
4
|
|
|
4
5
|
export default class CookieStorageEngine {
|
|
5
6
|
constructor(storageKey, options) {
|
|
@@ -18,6 +19,7 @@ export default class CookieStorageEngine {
|
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
removeToken() {
|
|
22
|
+
debugTmp('[CookieStorageEngine] removeToken');
|
|
21
23
|
return Cookie.remove(this.storageKey, getCookieOptions(this.options));
|
|
22
24
|
}
|
|
23
25
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import jwtDecode from 'jwt-decode';
|
|
2
|
+
import { debugTmp } from '../../utils';
|
|
2
3
|
|
|
3
4
|
export default class LocalStorageEngine {
|
|
4
5
|
constructor(storageKey) {
|
|
@@ -46,6 +47,7 @@ export default class LocalStorageEngine {
|
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
removeToken() {
|
|
50
|
+
debugTmp('[LocalStorageEngine] removeToken');
|
|
49
51
|
if (typeof window === 'undefined' || typeof window.localStorage === 'undefined') {
|
|
50
52
|
return undefined;
|
|
51
53
|
}
|