You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.1 KiB

  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useContext, useEffect, useRef } from 'react';
  16. import { useNavigate, useSearchParams } from 'react-router-dom';
  17. import { useTranslation } from 'react-i18next';
  18. import {
  19. API,
  20. showError,
  21. showSuccess,
  22. updateAPI,
  23. setUserData,
  24. } from '../../helpers';
  25. import { UserContext } from '../../context/User';
  26. import Loading from '../common/ui/Loading';
  27. const OAuth2Callback = (props) => {
  28. const { t } = useTranslation();
  29. const [searchParams] = useSearchParams();
  30. const [, userDispatch] = useContext(UserContext);
  31. const navigate = useNavigate();
  32. // 防止 React 18 Strict Mode 下重复执行
  33. const hasExecuted = useRef(false);
  34. // 最大重试次数
  35. const MAX_RETRIES = 3;
  36. const sendCode = async (code, state, retry = 0) => {
  37. try {
  38. const { data: resData } = await API.get(
  39. `/api/oauth/${props.type}?code=${code}&state=${state}`,
  40. );
  41. const { success, message, data } = resData;
  42. if (!success) {
  43. // 业务错误不重试,直接显示错误
  44. showError(message || t('授权失败'));
  45. return;
  46. }
  47. if (message === 'bind') {
  48. showSuccess(t('绑定成功!'));
  49. navigate('/console/personal');
  50. } else {
  51. userDispatch({ type: 'login', payload: data });
  52. localStorage.setItem('user', JSON.stringify(data));
  53. setUserData(data);
  54. updateAPI();
  55. showSuccess(t('登录成功!'));
  56. navigate('/console/token');
  57. }
  58. } catch (error) {
  59. // 网络错误等可重试
  60. if (retry < MAX_RETRIES) {
  61. // 递增的退避等待
  62. await new Promise((resolve) => setTimeout(resolve, (retry + 1) * 2000));
  63. return sendCode(code, state, retry + 1);
  64. }
  65. // 重试次数耗尽,提示错误并返回设置页面
  66. showError(error.message || t('授权失败'));
  67. navigate('/console/personal');
  68. }
  69. };
  70. useEffect(() => {
  71. // 防止 React 18 Strict Mode 下重复执行
  72. if (hasExecuted.current) {
  73. return;
  74. }
  75. hasExecuted.current = true;
  76. const code = searchParams.get('code');
  77. const state = searchParams.get('state');
  78. // 参数缺失直接返回
  79. if (!code) {
  80. showError(t('未获取到授权码'));
  81. navigate('/console/personal');
  82. return;
  83. }
  84. sendCode(code, state);
  85. }, []);
  86. return <Loading />;
  87. };
  88. export default OAuth2Callback;