Skip to content

Commit 5963e3b

Browse files
authored
Update callback.tsx
1 parent 7675fe9 commit 5963e3b

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

src/routes/callback.tsx

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { createFileRoute, useNavigate } from '@tanstack/react-router';
2-
import { useEffect } from 'react';
3-
2+
import { useEffect, useState } from 'react';
43

54
export const Route = createFileRoute('/callback')({
65
component: Callback,
76
})
87

98
function Callback() {
10-
const navigate = useNavigate()
9+
const navigate = useNavigate();
10+
const [userInfo, setUserInfo] = useState(null);
1111

1212
useEffect(() => {
1313
async function handleOAuthCallback() {
@@ -16,13 +16,13 @@ function Callback() {
1616

1717
if (!code) {
1818
console.error('授權碼缺失');
19-
window.location.href = '/' // 如果沒有授權碼,導回登入頁面
19+
window.location.href = '/'; // 如果沒有授權碼,導回登入頁面
2020
return;
2121
}
2222

2323
try {
2424
// 交換 access_token
25-
const response = await fetch('https://ncuapp.davidday.tw/oauth2/token', {
25+
const tokenResponse = await fetch('https://ncuapp.davidday.tw/oauth2/token', {
2626
method: 'POST',
2727
headers: {
2828
'Accept': 'application/json',
@@ -31,20 +31,34 @@ function Callback() {
3131
body: JSON.stringify({
3232
code,
3333
client_id: import.meta.env.VITE_NCU_PORTAL_CLIENT_ID,
34-
client_secret: import.meta.env.NCU_PORTAL_CLIENT_SECRET,
34+
client_secret: import.meta.env.VITE_NCU_PORTAL_CLIENT_SECRET,
3535
redirect_uri: 'https://ncuappteam.github.io/callback',
3636
grant_type: 'authorization_code'
3737
})
3838
});
3939

40-
const responseJson = await response.json();
41-
if (responseJson.access_token) {
42-
console.log('Access Token:', responseJson.access_token);
43-
} else {
44-
console.error('取得 access_token 失敗', responseJson);
40+
const tokenData = await tokenResponse.json();
41+
if (!tokenData.access_token) {
42+
console.error('取得 access_token 失敗', tokenData);
43+
return;
4544
}
4645

47-
navigate({ to: '/' });
46+
console.log('Access Token:', tokenData.access_token);
47+
48+
// 取得使用者資訊
49+
const userResponse = await fetch('https://portal.ncu.edu.tw/apis/oauth/v1/info', {
50+
method: 'GET',
51+
headers: {
52+
'Authorization': `Bearer ${tokenData.access_token}`,
53+
'Accept': 'application/json'
54+
}
55+
});
56+
57+
const userData = await userResponse.json();
58+
console.log('使用者資訊:', userData);
59+
setUserInfo(userData);
60+
61+
navigate({ to: '/' }); // 取得資料後導航到首頁
4862
} catch (error) {
4963
console.error('OAuth 登入失敗:', error);
5064
navigate({ to: '/' });
@@ -54,7 +68,17 @@ function Callback() {
5468
handleOAuthCallback();
5569
}, [navigate]);
5670

57-
return <div>正在處理登入...</div>;
71+
return (
72+
<div>
73+
<h2>正在處理登入...</h2>
74+
{userInfo && (
75+
<div>
76+
<h3>使用者資訊:</h3>
77+
<pre>{JSON.stringify(userInfo, null, 2)}</pre>
78+
</div>
79+
)}
80+
</div>
81+
);
5882
}
5983

6084
export default Callback;

0 commit comments

Comments
 (0)