OAuth2Callback.jsx 2.7 KB

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