@arcblock/did-connect-react 3.1.30 → 3.1.32
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/Connect/components/login-item/connect-choose-list.js +104 -102
- package/lib/Connect/plugins/email/list-item.js +13 -11
- package/lib/OAuth/context.js +98 -89
- package/lib/OAuth/passport-switcher.js +3 -112
- package/lib/Passkey/context.js +65 -54
- package/lib/Session/handler.js +55 -0
- package/lib/Session/hooks/use-session-token.js +109 -105
- package/lib/Session/index.js +455 -425
- package/lib/Session/libs/constants.js +5 -2
- package/lib/components/PassportSwitcher.js +128 -0
- package/lib/package.json.js +1 -1
- package/package.json +5 -5
- package/src/Connect/components/login-item/connect-choose-list.jsx +6 -0
- package/src/Connect/plugins/email/list-item.jsx +2 -0
- package/src/OAuth/context.jsx +15 -6
- package/src/OAuth/passport-switcher.jsx +2 -133
- package/src/Passkey/context.jsx +13 -5
- package/src/Session/handler.jsx +98 -0
- package/src/Session/hooks/use-session-token.js +12 -0
- package/src/Session/index.jsx +103 -31
- package/src/Session/libs/constants.js +3 -0
- package/src/components/PassportSwitcher.jsx +160 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ReactGA as t } from "@arcblock/ux/lib/withTracker";
|
|
2
|
+
import { GA_LAST_LOGIN_METHOD as n, GA_LAST_ROLE as l, GA_LAST_SOURCE_PROVIDER as i } from "@arcblock/ux/lib/withTracker/constant";
|
|
3
|
+
const S = () => {
|
|
4
|
+
const e = {
|
|
5
|
+
action: "loginSuccess",
|
|
6
|
+
provider: localStorage.getItem(n),
|
|
7
|
+
success: !0
|
|
8
|
+
};
|
|
9
|
+
t.event(e.action, e);
|
|
10
|
+
}, v = (e) => {
|
|
11
|
+
const s = {
|
|
12
|
+
action: "loginFailed",
|
|
13
|
+
provider: localStorage.getItem(n),
|
|
14
|
+
success: !1,
|
|
15
|
+
errorMessage: e.message
|
|
16
|
+
};
|
|
17
|
+
t.event(s.action, s);
|
|
18
|
+
}, p = (e, s, o) => {
|
|
19
|
+
const c = localStorage.getItem(l), r = o?.user?.role, a = {
|
|
20
|
+
action: "switchPassportSuccess",
|
|
21
|
+
change: `${c} -> ${r}`,
|
|
22
|
+
success: !0
|
|
23
|
+
};
|
|
24
|
+
t.event(a.action, a);
|
|
25
|
+
}, m = (e) => {
|
|
26
|
+
const s = {
|
|
27
|
+
action: "switchPassportFailed",
|
|
28
|
+
success: !1,
|
|
29
|
+
errorMessage: e.message
|
|
30
|
+
};
|
|
31
|
+
t.event(s.action, s);
|
|
32
|
+
}, _ = (e, s, o) => {
|
|
33
|
+
const c = {
|
|
34
|
+
action: "bindWalletSuccess",
|
|
35
|
+
provider: o?.user?.sourceProvider,
|
|
36
|
+
success: !0
|
|
37
|
+
};
|
|
38
|
+
t.event(c.action, c);
|
|
39
|
+
}, E = (e) => {
|
|
40
|
+
const s = {
|
|
41
|
+
action: "bindWalletFailed",
|
|
42
|
+
provider: localStorage.getItem(i),
|
|
43
|
+
success: !1,
|
|
44
|
+
errorMessage: e.message
|
|
45
|
+
};
|
|
46
|
+
t.event(s.action, s);
|
|
47
|
+
};
|
|
48
|
+
export {
|
|
49
|
+
E as gaBindWalletFailedHandler,
|
|
50
|
+
_ as gaBindWalletSuccessHandler,
|
|
51
|
+
v as gaLoginFailedHandler,
|
|
52
|
+
S as gaLoginSuccessHandler,
|
|
53
|
+
m as gaSwitchPassportFailedHandler,
|
|
54
|
+
p as gaSwitchPassportSuccessHandler
|
|
55
|
+
};
|
|
@@ -1,169 +1,173 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
import { useCreation as
|
|
3
|
-
import { joinURL as
|
|
4
|
-
import
|
|
1
|
+
import { useState as K } from "react";
|
|
2
|
+
import { useCreation as M, useMemoizedFn as c, useInterval as Z } from "ahooks";
|
|
3
|
+
import { joinURL as U, withQuery as H } from "ufo";
|
|
4
|
+
import k from "js-cookie";
|
|
5
5
|
import { setVisitorId as ee, getCookieOptions as re } from "@arcblock/ux/lib/Util";
|
|
6
|
-
import
|
|
7
|
-
import
|
|
6
|
+
import oe from "jwt-decode";
|
|
7
|
+
import ne from "@arcblock/ux/lib/Toast";
|
|
8
8
|
import se from "p-retry";
|
|
9
9
|
import te from "lodash/noop";
|
|
10
|
-
import ie from "
|
|
11
|
-
import {
|
|
12
|
-
import
|
|
13
|
-
|
|
10
|
+
import { ReactGA as ie } from "@arcblock/ux/lib/withTracker";
|
|
11
|
+
import { GA_LAST_SOURCE_PROVIDER as ce } from "@arcblock/ux/lib/withTracker/constant";
|
|
12
|
+
import fe from "../../Service/index.js";
|
|
13
|
+
import { createAxios as le, getBrowserLang as ue, logger as A, debug as u, sleep as ae, decrypt as de } from "../../utils.js";
|
|
14
|
+
import { CHECK_INTERVAL_TIME as m, LANG_COOKIE_NAME as he } from "../../constant.js";
|
|
15
|
+
class T extends Error {
|
|
14
16
|
}
|
|
15
|
-
const
|
|
16
|
-
function
|
|
17
|
+
const ke = 10, v = (e) => de(e, localStorage.getItem("__encKey"), localStorage.getItem("__decKey"));
|
|
18
|
+
function Ee({
|
|
17
19
|
state: e,
|
|
18
20
|
pageState: i,
|
|
19
|
-
serviceHost:
|
|
20
|
-
sessionTokenStorage:
|
|
21
|
-
refreshTokenStorage:
|
|
22
|
-
lazyRefreshToken:
|
|
23
|
-
apiOptions:
|
|
24
|
-
onRefresh:
|
|
21
|
+
serviceHost: p,
|
|
22
|
+
sessionTokenStorage: O,
|
|
23
|
+
refreshTokenStorage: V,
|
|
24
|
+
lazyRefreshToken: N,
|
|
25
|
+
apiOptions: b = {},
|
|
26
|
+
onRefresh: z = te
|
|
25
27
|
}) {
|
|
26
|
-
const [
|
|
27
|
-
baseURL:
|
|
28
|
+
const [q, R] = K(0), [P, d] = K(m), { getToken: y, setToken: C, removeToken: j } = O, { getToken: w, setToken: g, removeToken: B } = V, $ = M(() => le({
|
|
29
|
+
baseURL: p,
|
|
28
30
|
timeout: 10 * 1e3,
|
|
29
31
|
secure: !0
|
|
30
|
-
}), [
|
|
31
|
-
|
|
32
|
-
},
|
|
32
|
+
}), [p]), h = () => {
|
|
33
|
+
j(), B();
|
|
34
|
+
}, _ = M(() => fe(
|
|
33
35
|
{
|
|
34
|
-
sessionTokenStorage:
|
|
35
|
-
refreshTokenStorage:
|
|
36
|
-
serviceHost:
|
|
36
|
+
sessionTokenStorage: O,
|
|
37
|
+
refreshTokenStorage: V,
|
|
38
|
+
serviceHost: p,
|
|
37
39
|
authServicePrefix: i.prefix,
|
|
38
40
|
onRefreshTokenError() {
|
|
39
|
-
|
|
41
|
+
h(), e.user && (e.user = null);
|
|
40
42
|
}
|
|
41
43
|
},
|
|
42
|
-
|
|
43
|
-
), [
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
},
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
}),
|
|
50
|
-
const
|
|
44
|
+
b
|
|
45
|
+
), [p, i.prefix]), x = () => {
|
|
46
|
+
const o = re({ returnDomain: !1 });
|
|
47
|
+
k.remove("connected_did", o), k.remove("connected_pk", o), k.remove("connected_app", o), k.remove("connected_wallet_os", o), h();
|
|
48
|
+
}, Q = c(() => {
|
|
49
|
+
const o = k.get(he) || ue();
|
|
50
|
+
o !== i.currentLocale && (i.currentLocale = o);
|
|
51
|
+
}), X = c(async () => {
|
|
52
|
+
const o = () => _.get(U(i.notificationPrefix, "/unread-count"));
|
|
51
53
|
try {
|
|
52
|
-
const { data:
|
|
53
|
-
return
|
|
54
|
-
} catch (
|
|
55
|
-
return console.error("getUnreadCount error",
|
|
54
|
+
const { data: n } = await o();
|
|
55
|
+
return n.unReadCount;
|
|
56
|
+
} catch (n) {
|
|
57
|
+
return console.error("getUnreadCount error", n), 0;
|
|
56
58
|
}
|
|
57
|
-
}),
|
|
58
|
-
async ({ showProgress:
|
|
59
|
+
}), D = c(
|
|
60
|
+
async ({ showProgress: o = !1, requestFn: n = null, onlyRefreshToken: s = !1, type: t = "refreshToken" }) => {
|
|
59
61
|
try {
|
|
60
62
|
if (e.loading)
|
|
61
63
|
return console.warn("SessionProvider.refresh is currently in progress, call it will be noop"), { loading: !0 };
|
|
62
|
-
|
|
63
|
-
const { data:
|
|
64
|
-
if (
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
o && (e.loading = !0);
|
|
65
|
+
const { data: r, status: f } = await n(), a = r?.user?.did;
|
|
66
|
+
if (a && ie.set({
|
|
67
|
+
user_id: a
|
|
68
|
+
}), r?.user?.sourceProvider && localStorage.setItem(ce, r.user.sourceProvider), f === 400 && (h(), e.user = null, e.error = ""), r.error)
|
|
69
|
+
throw e.error = r.error, e.open = !1, e.unReadCount = 0, new Error(r.error);
|
|
70
|
+
if (r.user) {
|
|
67
71
|
try {
|
|
68
|
-
const
|
|
69
|
-
e.unReadCount =
|
|
70
|
-
} catch (
|
|
71
|
-
|
|
72
|
+
const l = await X();
|
|
73
|
+
e.unReadCount = l || 0;
|
|
74
|
+
} catch (l) {
|
|
75
|
+
A.error("getUnreadCount error", l), e.unReadCount = 0;
|
|
72
76
|
}
|
|
73
|
-
|
|
77
|
+
r.nextToken && (C(r.nextToken), z({
|
|
74
78
|
type: t,
|
|
75
|
-
sessionToken:
|
|
76
|
-
refreshToken:
|
|
77
|
-
user:
|
|
78
|
-
}),
|
|
79
|
+
sessionToken: r.nextToken,
|
|
80
|
+
refreshToken: r.nextRefreshToken,
|
|
81
|
+
user: r.user
|
|
82
|
+
}), r.nextRefreshToken && g(r.nextRefreshToken)), s || (e.open = !1, e.user = r.user, e.provider = r.provider, e.walletOS = r.walletOS), y() || ne.error("Can't write session token, please use https url to access this page");
|
|
79
83
|
} else
|
|
80
|
-
throw
|
|
81
|
-
return { data:
|
|
82
|
-
} catch (
|
|
83
|
-
throw
|
|
84
|
+
throw o && (e.loading = i.autoConnect), e.open = i.autoConnect, e.user = null, e.provider = "", e.walletOS = "", e.unReadCount = 0, new T("Invalid token");
|
|
85
|
+
return { data: r, status: f };
|
|
86
|
+
} catch (r) {
|
|
87
|
+
throw A.error("SessionProvider.refresh error", r), e.open = !1, e.error = r.message, r?.response?.status === 400 ? new T("Invalid token") : r;
|
|
84
88
|
} finally {
|
|
85
|
-
|
|
89
|
+
o && (e.loading = !1);
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
|
-
),
|
|
89
|
-
u("handleRefreshToken", { showProgress:
|
|
90
|
-
const s = () =>
|
|
92
|
+
), I = c(async (o = !1, n = !1) => {
|
|
93
|
+
u("handleRefreshToken", { showProgress: o, onlyRefreshToken: n });
|
|
94
|
+
const s = () => $.post(H(U(i.prefix, "/refreshSession")), null, {
|
|
91
95
|
headers: {
|
|
92
|
-
authorization: `Bearer ${encodeURIComponent(
|
|
96
|
+
authorization: `Bearer ${encodeURIComponent(w())}`
|
|
93
97
|
}
|
|
94
98
|
});
|
|
95
|
-
|
|
99
|
+
d(void 0);
|
|
96
100
|
try {
|
|
97
|
-
await
|
|
101
|
+
await D({ showProgress: o, requestFn: s, onlyRefreshToken: n, type: "refreshToken" }), R(0), d(m);
|
|
98
102
|
} catch (t) {
|
|
99
|
-
t instanceof
|
|
103
|
+
t instanceof T ? (console.warn("refresh token failed, remove all tokens"), R(0), h(), d(m)) : (A.error("refresh token failed, unexpected error:", t), R(q + 1), await ae(m), q >= ke ? (R(0), d(void 0)) : d(m));
|
|
100
104
|
}
|
|
101
|
-
}),
|
|
102
|
-
if (
|
|
105
|
+
}), F = c((o = !1) => {
|
|
106
|
+
if (N)
|
|
103
107
|
return;
|
|
104
|
-
const
|
|
105
|
-
let s =
|
|
108
|
+
const n = y();
|
|
109
|
+
let s = o;
|
|
106
110
|
const t = (/* @__PURE__ */ new Date()).getTime();
|
|
107
|
-
if (
|
|
111
|
+
if (n)
|
|
108
112
|
try {
|
|
109
|
-
const { exp:
|
|
110
|
-
|
|
113
|
+
const { exp: r, iat: f } = oe(n);
|
|
114
|
+
r * 1e3 - t < t - f * 1e3 && (s = !0);
|
|
111
115
|
} catch {
|
|
112
116
|
s = !0;
|
|
113
117
|
}
|
|
114
118
|
else
|
|
115
119
|
s = !0;
|
|
116
|
-
s && (
|
|
117
|
-
}),
|
|
118
|
-
const s = () => se(() =>
|
|
120
|
+
s && (w() ? I(!1, !0) : (x(), e.user && (e.user = null)));
|
|
121
|
+
}), G = c(async ({ showProgress: o = !1, forceRefreshToken: n = !1 } = {}) => {
|
|
122
|
+
const s = () => se(() => _.get(U(i.prefix, "/session"), { secure: !0 }), {
|
|
119
123
|
retries: 2
|
|
120
124
|
});
|
|
121
|
-
u("handleRefreshUser", { showProgress:
|
|
125
|
+
u("handleRefreshUser", { showProgress: o, forceRefreshToken: n });
|
|
122
126
|
try {
|
|
123
|
-
if (
|
|
124
|
-
throw new
|
|
125
|
-
await
|
|
127
|
+
if (n)
|
|
128
|
+
throw new T("need force refresh token");
|
|
129
|
+
await D({ showProgress: o, requestFn: s, type: "refreshUser" });
|
|
126
130
|
} catch (t) {
|
|
127
|
-
t instanceof
|
|
131
|
+
t instanceof T && (u("handleRefreshUser failed, try to refresh token", { err: t }), await I());
|
|
128
132
|
}
|
|
129
|
-
}),
|
|
130
|
-
const
|
|
131
|
-
e.user || (
|
|
132
|
-
}),
|
|
133
|
+
}), Y = c(async () => {
|
|
134
|
+
const o = y(), n = w();
|
|
135
|
+
e.user || (o || n ? await G() : (x(), e.user && (e.user = null)));
|
|
136
|
+
}), J = c(() => {
|
|
133
137
|
if (e.initialized && e.user)
|
|
134
138
|
try {
|
|
135
|
-
|
|
139
|
+
F();
|
|
136
140
|
} catch {
|
|
137
141
|
}
|
|
138
|
-
}), W = c((
|
|
139
|
-
const
|
|
140
|
-
u("handleLoginResult", { loginResult:
|
|
141
|
-
const { loginToken: s, sessionToken: t, refreshToken:
|
|
142
|
-
let L,
|
|
143
|
-
|
|
142
|
+
}), W = c((o) => {
|
|
143
|
+
const n = Array.isArray(o) ? o[0] : o;
|
|
144
|
+
u("handleLoginResult", { loginResult: n, result: o });
|
|
145
|
+
const { loginToken: s, sessionToken: t, refreshToken: r, visitorId: f, encrypted: a = !0 } = n, l = s || t;
|
|
146
|
+
let L, S, E;
|
|
147
|
+
l && (L = a ? v(l) : l, u("handleLoginResult: setSessionToken", { decryptSessionToken: L }), C(L), r && (S = a ? v(r) : r, u("handleLoginResult: setRefreshToken", { decryptRefreshToken: S }), g(S))), f && (E = a ? v(f) : f, u("handleLoginResult: setVisitorId", { decryptVisitorId: E }), ee(E));
|
|
144
148
|
});
|
|
145
149
|
return Z(
|
|
146
150
|
() => {
|
|
147
|
-
|
|
151
|
+
Q(), F();
|
|
148
152
|
},
|
|
149
|
-
|
|
153
|
+
P,
|
|
150
154
|
{ immediate: !0 }
|
|
151
155
|
), {
|
|
152
|
-
renewToken:
|
|
153
|
-
handleRefreshUser:
|
|
154
|
-
handleRefreshToken:
|
|
155
|
-
syncSessionSate:
|
|
156
|
+
renewToken: J,
|
|
157
|
+
handleRefreshUser: G,
|
|
158
|
+
handleRefreshToken: I,
|
|
159
|
+
syncSessionSate: Y,
|
|
156
160
|
handleLoginResult: W,
|
|
157
|
-
decrypt:
|
|
158
|
-
removeToken:
|
|
161
|
+
decrypt: v,
|
|
162
|
+
removeToken: h,
|
|
159
163
|
clearSession: x,
|
|
160
164
|
getSessionToken: y,
|
|
161
|
-
getRefreshToken:
|
|
165
|
+
getRefreshToken: w,
|
|
162
166
|
setRefreshToken: g,
|
|
163
167
|
setSessionToken: C,
|
|
164
|
-
service:
|
|
168
|
+
service: _
|
|
165
169
|
};
|
|
166
170
|
}
|
|
167
171
|
export {
|
|
168
|
-
|
|
172
|
+
Ee as default
|
|
169
173
|
};
|