-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from gary-Shen/feat/v2.0.0
fix: github api redirect
- Loading branch information
Showing
1 changed file
with
32 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,38 @@ | ||
function createCodeHandler(oauthHandler) { | ||
return async function handleCode(request, response) { | ||
const { code, extension_id } = request.query; | ||
const fetch = require('node-fetch'); | ||
const { Redis } = require('@upstash/redis'); | ||
|
||
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); | ||
const { createCodeHandler } = require('./utils'); | ||
|
||
response.redirect(`chrome-extension://${extension_id}/index.html?token=${accessToken}`); | ||
} 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(); | ||
|
||
module.exports = { | ||
createCodeHandler, | ||
sendRejection, | ||
}; | ||
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; | ||
}); |
7e713d2
There was a problem hiding this comment.
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.vercel.app
candi-tab-gary-shen.vercel.app