Skip to content

Commit 74af854

Browse files
committed
feat: add gitee token process
Signed-off-by: frank-zsy <[email protected]>
1 parent 35fdafd commit 74af854

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/pages/Options/components/GiteeToken.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,54 @@ const GiteeToken = () => {
2020
fetchToken();
2121
}, []);
2222

23+
const handleOAuthToken = async () => {
24+
const clientId = 'e76727820aa539f3a59399d0bc48156df2057e81774617e433eeb49d1dad97b3';
25+
const redirectUri = chrome.identity.getRedirectURL();
26+
const scope = encodeURIComponent('user_info');
27+
const authUrl = `https://gitee.com/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code`;
28+
29+
console.log('[FastPR]: Start authorization...');
30+
chrome.identity.launchWebAuthFlow(
31+
{
32+
url: authUrl,
33+
interactive: true,
34+
},
35+
function (redirectUrl) {
36+
if (chrome.runtime.lastError || !redirectUrl) {
37+
console.error(chrome.runtime.lastError ? chrome.runtime.lastError.message : 'Authorization failed.');
38+
return;
39+
}
40+
const code = new URL(redirectUrl).searchParams.get('code');
41+
console.log('[FastPR]: Get session code:', code?.slice(0, 2).padEnd(code.length - 2, '*'));
42+
fetch('http://8.147.129.123/gitee', {
43+
method: 'POST',
44+
headers: {
45+
'Content-Type': 'application/json',
46+
Accept: 'application/json',
47+
},
48+
body: JSON.stringify({ code }),
49+
}).then(async (response) => {
50+
if (!response.ok) {
51+
console.error(`HTTP error! status: ${response.status}`);
52+
}
53+
const respData = await response.json();
54+
console.log(respData);
55+
const token = respData.access_token;
56+
57+
const userData = await giteeRequest('/user', {
58+
headers: { access_token: `Bearer ${token}` },
59+
});
60+
61+
if (userData === null || userData.message) {
62+
showMessage(t('gitee_token_error_invalid'), 'error');
63+
} else {
64+
showMessage(t('gitee_token_success_valid', { username: userData.login }), 'success');
65+
}
66+
});
67+
}
68+
);
69+
};
70+
2371
const handleSave = () => {
2472
if (!token.trim()) {
2573
showMessage(t('gitee_token_error_empty'), 'error');
@@ -125,6 +173,9 @@ const GiteeToken = () => {
125173
<button onClick={handleTestToken} style={{ marginTop: '17px' }}>
126174
{t('gitee_token_test')}
127175
</button>
176+
<button onClick={handleOAuthToken} style={{ marginTop: '17px' }}>
177+
{'Start OAuth'}
178+
</button>
128179
</div>
129180
</div>
130181
);

0 commit comments

Comments
 (0)