@@ -44,15 +44,18 @@ function App() {
4444
4545 baseURL = `${ window . location . protocol } //${ window . location . hostname } :${ listen_port } ` ;
4646
47- // 首先尝试访问主页,检查是否已经登录
48- try {
49- const checkResponse = await axios . get ( baseURL + '/w9git/explore/repos' , {
50- withCredentials : true ,
51- maxRedirects : 0 ,
52- validateStatus : ( status ) => status >= 200 && status < 400
53- } ) ;
47+ // 获取登录页面和 CSRF token
48+ const response = await axios . get ( baseURL + '/w9git/user/login' , {
49+ withCredentials : true
50+ } ) ;
51+ const doc = new DOMParser ( ) . parseFromString ( response . data , 'text/html' ) ;
5452
55- // 如果能成功访问,说明已经登录,直接设置 iframe
53+ // 获取 CSRF token
54+ const csrfElement = doc . querySelector ( 'input[name="_csrf"]' ) ;
55+
56+ if ( ! csrfElement ) {
57+ // 可能页面被重定向了(已登录),尝试直接加载 iframe
58+ console . log ( 'No CSRF token found, attempting to load iframe anyway' ) ;
5659 setIframeKey ( Math . random ( ) ) ;
5760 var newHash = window . location . hash ;
5861 if ( newHash . includes ( `/w9git/${ userName } ` ) ) {
@@ -65,24 +68,6 @@ function App() {
6568 setIframeSrc ( baseURL + '/w9git/explore/repos' ) ;
6669 }
6770 return ;
68- } catch ( checkError ) {
69- // 如果未登录或出错,继续执行登录流程
70- console . log ( 'Not logged in, proceeding with login...' ) ;
71- }
72-
73- // 获取登录页面和 CSRF token
74- const response = await axios . get ( baseURL + '/w9git/user/login' , {
75- withCredentials : true
76- } ) ;
77- const doc = new DOMParser ( ) . parseFromString ( response . data , 'text/html' ) ;
78-
79- // 获取 CSRF token
80- const csrfElement = doc . querySelector ( 'input[name="_csrf"]' ) ;
81-
82- if ( ! csrfElement ) {
83- setShowAlert ( true ) ;
84- setAlertMessage ( "Failed to get CSRF token from login page. Please check if Gitea is running properly." ) ;
85- return ;
8671 }
8772
8873 const csrf = csrfElement . value ;
0 commit comments