OAuth2Callback.js 2.7 KB

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