@tsingroc/tsingroc-components 5.0.0-alpha.19 → 5.0.0-alpha.20
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/components/Auth.js +43 -15
- package/package.json +1 -1
package/dist/components/Auth.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { c as _c } from "react/compiler-runtime";
|
|
1
2
|
import Sdk from "casdoor-js-sdk";
|
|
2
3
|
import { jwtDecode } from "jwt-decode";
|
|
3
|
-
import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { createContext, useContext, useEffect, useEffectEvent, useMemo, useRef, useState } from "react";
|
|
4
5
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
5
6
|
const commonAuth = (user, setUser) => ({
|
|
6
7
|
logout(redirect) {
|
|
@@ -215,29 +216,56 @@ export function AuthProvider(props) {
|
|
|
215
216
|
* 该组件必须在 {@linkcode AuthProvider} 内部才能使用。
|
|
216
217
|
*/
|
|
217
218
|
export function AuthCheck(props) {
|
|
219
|
+
const $ = _c(8);
|
|
218
220
|
const auth = useAuth("AuthCheck");
|
|
219
221
|
const userInfo = auth.userInfo;
|
|
220
|
-
// 用于确保在 Strict Mode 下副作用也只执行一次
|
|
221
222
|
const effectDone = useRef(false);
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
223
|
+
let t0;
|
|
224
|
+
if ($[0] !== auth || $[1] !== props.onFail) {
|
|
225
|
+
t0 = () => {
|
|
226
|
+
auth.validate().catch(props.onFail ?? (() => auth.login(location.href)));
|
|
227
|
+
};
|
|
228
|
+
$[0] = auth;
|
|
229
|
+
$[1] = props.onFail;
|
|
230
|
+
$[2] = t0;
|
|
231
|
+
} else {
|
|
232
|
+
t0 = $[2];
|
|
233
|
+
}
|
|
234
|
+
const onUnloggedIn = useEffectEvent(t0);
|
|
235
|
+
let t1;
|
|
236
|
+
if ($[3] !== auth.userInfo || $[4] !== onUnloggedIn) {
|
|
237
|
+
t1 = () => {
|
|
238
|
+
if (effectDone.current) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
effectDone.current = true;
|
|
242
|
+
if (!auth.userInfo) {
|
|
243
|
+
onUnloggedIn();
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
$[3] = auth.userInfo;
|
|
247
|
+
$[4] = onUnloggedIn;
|
|
248
|
+
$[5] = t1;
|
|
249
|
+
} else {
|
|
250
|
+
t1 = $[5];
|
|
251
|
+
}
|
|
252
|
+
let t2;
|
|
253
|
+
if ($[6] !== auth) {
|
|
254
|
+
t2 = [auth];
|
|
255
|
+
$[6] = auth;
|
|
256
|
+
$[7] = t2;
|
|
257
|
+
} else {
|
|
258
|
+
t2 = $[7];
|
|
259
|
+
}
|
|
260
|
+
useEffect(t1, t2);
|
|
233
261
|
if (userInfo) {
|
|
234
262
|
if (!("roles" in userInfo) || (props.roles ?? []).every(role => userInfo.roles.includes(role))) {
|
|
235
263
|
return props.children;
|
|
236
264
|
} else {
|
|
237
|
-
return props.forbidden ?? "
|
|
265
|
+
return props.forbidden ?? "\u60A8\u65E0\u6743\u8BBF\u95EE\u6B64\u9875\u9762\uFF01";
|
|
238
266
|
}
|
|
239
267
|
} else {
|
|
240
|
-
return props.loading ?? "
|
|
268
|
+
return props.loading ?? "\u6B63\u5728\u767B\u5F55\u2026\u2026";
|
|
241
269
|
}
|
|
242
270
|
}
|
|
243
271
|
/**
|