Skip to content

Commit d9aa53f

Browse files
committed
update callback.tsx
1 parent ecef46a commit d9aa53f

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed

src/routes/callback.tsx

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,50 @@ 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('授權碼缺失');
20-
window.location.href = '/' // 如果沒有授權碼,導回登入頁面
19+
window.location.href = '/'; // 如果沒有授權碼,導回登入頁面
2120
return;
2221
}
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 tokenResponse = 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({
32+
code,
33+
client_id: import.meta.env.VITE_NCU_PORTAL_CLIENT_ID,
34+
client_secret: import.meta.env.VITE_NCU_PORTAL_CLIENT_SECRET,
35+
redirect_uri: 'https://ncuappteam.github.io/callback',
36+
grant_type: 'authorization_code'
37+
})
38+
});
4639

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 失敗');
40+
const tokenData = await tokenResponse.json();
41+
if (!tokenData.access_token) {
42+
console.error('取得 access_token 失敗', tokenData);
43+
return;
6144
}
6245

63-
// 跳轉到首頁或其他頁面
64-
navigate({ to: '/' });
46+
// 取得使用者資訊
47+
const userResponse = await fetch('https://ncuapp.davidday.tw/oauth2/userinfo', {
48+
method: 'GET',
49+
headers: {
50+
'Authorization': `Bearer ${tokenData.access_token}`,
51+
'Accept': 'application/json'
52+
}
53+
});
54+
55+
const userData = await userResponse.json();
56+
console.log('使用者資訊:', userData);
57+
setUserInfo(userData);
58+
59+
navigate({ to: '/' }); // 取得資料後導航到首頁
6560
} catch (error) {
6661
console.error('OAuth 登入失敗:', error);
6762
navigate({ to: '/' });
@@ -84,4 +79,4 @@ function Callback() {
8479
);
8580
}
8681

87-
export default Callback;
82+
export default Callback;

0 commit comments

Comments
 (0)