@@ -13,7 +13,6 @@ function Callback() {
1313 async function handleOAuthCallback ( ) {
1414 const urlParams = new URLSearchParams ( window . location . search ) ;
1515 const code = urlParams . get ( 'code' ) ; // 取得 OAuth 授權碼
16- console . log ( 'code:' , code ) ;
1716
1817 if ( ! code ) {
1918 console . error ( '授權碼缺失' ) ;
@@ -23,44 +22,22 @@ function Callback() {
2322
2423 try {
2524 // 交換 access_token
26- const headers : Headers = new Headers ( ) ;
27- const client_id = import . meta. env . VITE_NCU_PORTAL_CLIENT_ID ;
28- const client_secret = import . meta. env . VITE_NCU_PORTAL_CLIENT_SECRET ;
29- const authHeader = btoa ( `${ client_id } :${ client_secret } ` ) ;
30- headers . append ( 'Accept' , 'application/json' ) ;
31- headers . append ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
32- headers . append ( 'Authorization' , `Basic ${ authHeader } ` ) ;
33-
34- const body = new URLSearchParams ( {
35- code : code ,
36- redirect_uri : 'http://ncuappteam.github.io/callback' ,
37- grant_type : 'authorization_code' ,
38- } ) ;
39- const request : RequestInfo = new Request ( 'https://ncuapp.davidday.tw/oauth2/token' , {
40- // We need to set the `method` to `POST` and assign the headers
25+ const response = await fetch ( 'https://ncuapp.davidday.tw/oauth2/token' , {
4126 method : 'POST' ,
42- headers : headers ,
43- // Convert the user object to JSON and pass it as the body
44- body : body . toString ( ) ,
45- } )
27+ headers : {
28+ 'Accept' : 'application/json' ,
29+ 'Content-Type' : 'application/json'
30+ } ,
31+ body : JSON . stringify ( { code } )
32+ } ) ;
4633
47- // const tokenResponse = await fetch('', {
48- // method: 'POST',
49- // headers: headers,
50- // body: body.toString(),
51- // });
52- const responseData = await fetch ( request ) ;
53- console . log ( 'responseData:' , responseData ) ;
54- const responseJson = await responseData . json ( )
55- if ( responseJson ) {
56- // localStorage.setItem('accessToken', accessToken);
57- console . log ( 'accessToken:' , responseJson ) ;
58- }
59- else {
60- console . error ( '取得 access_token 失敗' ) ;
34+ const responseJson = await response . json ( ) ;
35+ if ( responseJson . access_token ) {
36+ console . log ( 'Access Token:' , responseJson . access_token ) ;
37+ } else {
38+ console . error ( '取得 access_token 失敗' , responseJson ) ;
6139 }
6240
63- // 跳轉到首頁或其他頁面
6441 navigate ( { to : '/' } ) ;
6542 } catch ( error ) {
6643 console . error ( 'OAuth 登入失敗:' , error ) ;
0 commit comments