Skip to content

Commit

Permalink
Merge pull request #17 from gary-Shen/fix/github
Browse files Browse the repository at this point in the history
fix: fix server error
  • Loading branch information
gary-Shen authored Jun 24, 2023
2 parents 7e713d2 + b3aabdb commit 8a069ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
7 changes: 4 additions & 3 deletions server/api/github.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fetch = require('node-fetch');
const { Redis } = require('@upstash/redis');

const { createCodeHandler } = require('./utils');

module.exports = createCodeHandler(async (code) => {
const { CLIENT_ID, CLIENT_SECRET, UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN } = process.env;
module.exports = createCodeHandler(async (code, uuid) => {
const { CLIENT_ID, CLIENT_SECRET } = process.env;

const res = await fetch('https://github.com/login/oauth/access_token', {
headers: {
'Content-Type': 'application/json',
Expand All @@ -25,6 +25,7 @@ module.exports = createCodeHandler(async (code) => {
if (errorDescription) {
throw new Error(errorDescription);
} else if (scope !== 'gist' || !accessToken || !(typeof accessToken === 'string')) {
console.log(JSON.stringify(body));
throw new Error(`Cannot resolve response from GitHub`);
}

Expand Down
70 changes: 37 additions & 33 deletions server/api/utils.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
const fetch = require('node-fetch');
const { Redis } = require('@upstash/redis');
function createCodeHandler(oauthHandler) {
return async function handleCode(request, response) {
const { code, uuid } = request.query;

const { createCodeHandler } = require('./utils');
console.log('code', code);
try {
setCORSHeaders(response);
if (!request.method || request.method.toLowerCase() !== 'get') {
return sendRejection(response, 405);
}
if (!code || typeof code !== 'string') {
return sendRejection(response, 403);
}
const accessToken = await oauthHandler(code, uuid);
writeJSON(response, { accessToken });
response.end();
} catch (err) {
return sendRejection(response, 400, err instanceof Error ? err.message : '');
}
};
}

module.exports = createCodeHandler(async (code, uuid) => {
const { CLIENT_ID, CLIENT_SECRET, UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN } = process.env;
function setCORSHeaders(response) {
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', 'POST');
response.setHeader('Access-Control-Allow-Methods', 'GET');
}

console.log('fetching');
function sendRejection(response, status = 400, content) {
response.writeHead(status);
response.end(content);
}

const res = await fetch('https://github.com/login/oauth/access_token', {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
redirect: 'follow',
method: 'post',
body: JSON.stringify({
code,
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
}),
});
function writeJSON(response, data) {
response.setHeader('Content-Type', 'application/json');
response.write(JSON.stringify(data));
}

const body = await res.json();

const { access_token: accessToken, scope, error_description: errorDescription } = body;
if (errorDescription) {
throw new Error(errorDescription);
} else if (scope !== 'gist' || !accessToken || !(typeof accessToken === 'string')) {
console.log(JSON.stringify(body));
throw new Error(`Cannot resolve response from GitHub`);
}

// await redis.set(uuid, accessToken);
// console.log('redis token', await redis.set(uuid));
return accessToken;
});
module.exports = {
createCodeHandler,
sendRejection,
};

1 comment on commit 8a069ca

@vercel
Copy link

@vercel vercel bot commented on 8a069ca Jun 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

candi-tab – ./

candi-tab-git-master-gary-shen.vercel.app
candi-tab-gary-shen.vercel.app
candi-tab.vercel.app

Please sign in to comment.