Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ottmartens committed Nov 12, 2023
1 parent 26ce8fb commit 0eb97f0
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,41 @@ async function handle(request: Request) {
case '/callback':
return await fetchAccessToken(params);
}

return new Response();
}

async function fetchAccessToken(requestParams: URLSearchParams) {
const code = requestParams.get('code');

const response = await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: {
'content-type': 'application/json',
'user-agent': 'decap-cms-github-oauth-api-cloudflare',
accept: 'application/json',
},
body: JSON.stringify({ client_id, client_secret, code }),
}).then((res) => res.json());

const loginResponse = decapCMSLoginScript(response.access_token);

return new Response(loginResponse, {
status: 201,
headers: {
'Content-Type': 'text/html;charset=UTF-8',
},
});
try {
const code = requestParams.get('code');

const response = await fetch(
'https://github.com/login/oauth/access_token',
{
method: 'POST',
headers: {
'content-type': 'application/json',
'user-agent': 'decap-cms-github-oauth-api-cloudflare',
accept: 'application/json',
},
body: JSON.stringify({ client_id, client_secret, code }),
}
).then((res) => res.json());

const loginResponse = decapCMSLoginScript(response.access_token);

return new Response(loginResponse, {
status: 201,
headers: {
'Content-Type': 'text/html;charset=UTF-8',
},
});
} catch (err) {
console.error(err);
return new Response(err.message, {
status: 500,
});
}
}

function redirectToAuthFlow() {
Expand Down

0 comments on commit 0eb97f0

Please sign in to comment.