Jelajahi Sumber

fix: first login error (close #385)

1808837298@qq.com 1 tahun lalu
induk
melakukan
caaecb8d54
2 mengubah file dengan 14 tambahan dan 2 penghapusan
  1. 2 1
      web/src/components/LoginForm.js
  2. 12 1
      web/src/helpers/api.js

+ 2 - 1
web/src/components/LoginForm.js

@@ -1,7 +1,7 @@
 import React, { useContext, useEffect, useState } from 'react';
 import { Link, useNavigate, useSearchParams } from 'react-router-dom';
 import { UserContext } from '../context/User';
-import { API, getLogo, showError, showInfo, showSuccess } from '../helpers';
+import { API, getLogo, showError, showInfo, showSuccess, updateAPI } from '../helpers';
 import { onGitHubOAuthClicked } from './utils';
 import Turnstile from 'react-turnstile';
 import {
@@ -101,6 +101,7 @@ const LoginForm = () => {
       if (success) {
         userDispatch({ type: 'login', payload: data });
         setUserData(data);
+        updateAPI()
         showSuccess('登录成功!');
         if (username === 'root' && password === '123456') {
           Modal.error({

+ 12 - 1
web/src/helpers/api.js

@@ -1,7 +1,7 @@
 import { getUserIdFromLocalStorage, showError } from './utils';
 import axios from 'axios';
 
-export const API = axios.create({
+export let API = axios.create({
   baseURL: import.meta.env.VITE_REACT_APP_SERVER_URL
     ? import.meta.env.VITE_REACT_APP_SERVER_URL
     : '',
@@ -10,6 +10,17 @@ export const API = axios.create({
   }
 });
 
+export function updateAPI() {
+  API = axios.create({
+    baseURL: import.meta.env.VITE_REACT_APP_SERVER_URL
+      ? import.meta.env.VITE_REACT_APP_SERVER_URL
+      : '',
+    headers: {
+      'New-API-User': getUserIdFromLocalStorage()
+    }
+  });
+}
+
 API.interceptors.response.use(
   (response) => response,
   (error) => {