MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / OAuth2Callback

Function OAuth2Callback

web/src/components/auth/OAuth2Callback.js:8–68  ·  view source on GitHub ↗
(props)

Source from the content-addressed store, hash-verified

6import Loading from '../common/Loading';
7
8const OAuth2Callback = (props) => {
9 const { t } = useTranslation();
10 const [searchParams] = useSearchParams();
11 const [, userDispatch] = useContext(UserContext);
12 const navigate = useNavigate();
13
14 // 最大重试次数
15 const MAX_RETRIES = 3;
16
17 const sendCode = async (code, state, retry = 0) => {
18 try {
19 const { data: resData } = await API.get(
20 `/api/oauth/${props.type}?code=${code}&state=${state}`,
21 );
22
23 const { success, message, data } = resData;
24
25 if (!success) {
26 throw new Error(message || 'OAuth2 callback error');
27 }
28
29 if (message === 'bind') {
30 showSuccess(t('绑定成功!'));
31 navigate('/console/personal');
32 } else {
33 userDispatch({ type: 'login', payload: data });
34 localStorage.setItem('user', JSON.stringify(data));
35 setUserData(data);
36 updateAPI();
37 showSuccess(t('登录成功!'));
38 navigate('/console/token');
39 }
40 } catch (error) {
41 if (retry < MAX_RETRIES) {
42 // 递增的退避等待
43 await new Promise((resolve) => setTimeout(resolve, (retry + 1) * 2000));
44 return sendCode(code, state, retry + 1);
45 }
46
47 // 重试次数耗尽,提示错误并返回设置页面
48 showError(error.message || t('授权失败'));
49 navigate('/console/personal');
50 }
51 };
52
53 useEffect(() => {
54 const code = searchParams.get('code');
55 const state = searchParams.get('state');
56
57 // 参数缺失直接返回
58 if (!code) {
59 showError(t('未获取到授权码'));
60 navigate('/console/personal');
61 return;
62 }
63
64 sendCode(code, state);
65 }, []);

Callers

nothing calls this directly

Calls 3

showErrorFunction · 0.90
sendCodeFunction · 0.85
tFunction · 0.50

Tested by

no test coverage detected