Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"matches": ["<all_urls>"]
}
],
"permissions": ["storage"],
"permissions": ["storage", "identity"],
"host_permissions": ["<all_urls>"],
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self';"
Expand Down
51 changes: 51 additions & 0 deletions src/pages/Options/components/GitHubToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,54 @@ const GitHubToken = () => {
fetchToken();
}, []);

const handleOAuthToken = async () => {
const clientId = 'Ov23liyofMsuQYwtfGLb';
const redirectUri = chrome.identity.getRedirectURL();
const scope = encodeURIComponent('read:user');
const authUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=token`;

console.log('[FastPR]: Start authorization...');
chrome.identity.launchWebAuthFlow(
{
url: authUrl,
interactive: true,
},
function (redirectUrl) {
if (chrome.runtime.lastError || !redirectUrl) {
console.error(chrome.runtime.lastError ? chrome.runtime.lastError.message : 'Authorization failed.');
return;
}
const code = new URL(redirectUrl).searchParams.get('code');
console.log('[FastPR]: Get session code:', code?.slice(0, 2).padEnd(code.length - 2, '*'));
fetch('http://8.147.129.123/github', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({ code }),
}).then(async (response) => {
if (!response.ok) {
console.error(`HTTP error! status: ${response.status}`);
}
const respData = await response.json();
const token = respData.access_token;

const userDataReq = await fetch(`https://api.github.com/user`, {
headers: { Authorization: `Bearer ${token}` },
});

const userData = await userDataReq.json();
if (userData === null || userData.message) {
showMessage(t('github_token_error_invalid'), 'error');
} else {
console.log(`[FastPR]: Welcome: ${userData.login}`);
}
});
}
);
};

const handleSave = () => {
if (!token.trim()) {
showMessage(t('github_token_error_empty'), 'error');
Expand Down Expand Up @@ -128,6 +176,9 @@ const GitHubToken = () => {
<button onClick={handleTestToken} style={{ marginTop: '17px' }}>
{t('github_token_test')}
</button>
<button onClick={handleOAuthToken} style={{ marginTop: '17px' }}>
{'Start OAuth'}
</button>
</div>
</div>
);
Expand Down