Created
November 19, 2024 05:54
-
-
Save lukas-h/33739fc3e01d8c3c16f4dddff500116d to your computer and use it in GitHub Desktop.
DecapCMS Cloudflare Functions
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
export async function onRequest(context) { | |
const { | |
request, // same as existing Worker API | |
env, // same as existing Worker API | |
params, // if filename includes [id] or [[path]] | |
waitUntil, // same as ctx.waitUntil in existing Worker API | |
next, // used for middleware or to fetch assets | |
data, // arbitrary space for passing data between middlewares | |
} = context; | |
const client_id = env.GITHUB_CLIENT_ID; | |
try { | |
const url = new URL(request.url); | |
const redirectUrl = new URL('https://github.com/login/oauth/authorize'); | |
redirectUrl.searchParams.set('client_id', client_id); | |
redirectUrl.searchParams.set('redirect_uri', url.origin + '/api/callback'); | |
redirectUrl.searchParams.set('scope', 'repo user'); | |
redirectUrl.searchParams.set( | |
'state', | |
crypto.getRandomValues(new Uint8Array(12)).join(''), | |
); | |
return Response.redirect(redirectUrl.href, 301); | |
} catch (error) { | |
console.error(error); | |
return new Response(error.message, { | |
status: 500, | |
}); | |
} | |
} |
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
function renderBody(status, content) { | |
const html = ` | |
<script> | |
const receiveMessage = (message) => { | |
window.opener.postMessage( | |
'authorization:github:${status}:${JSON.stringify(content)}', | |
message.origin | |
); | |
window.removeEventListener("message", receiveMessage, false); | |
} | |
window.addEventListener("message", receiveMessage, false); | |
window.opener.postMessage("authorizing:github", "*"); | |
</script> | |
`; | |
const blob = new Blob([html]); | |
return blob; | |
} | |
export async function onRequest(context) { | |
const { | |
request, // same as existing Worker API | |
env, // same as existing Worker API | |
params, // if filename includes [id] or [[path]] | |
waitUntil, // same as ctx.waitUntil in existing Worker API | |
next, // used for middleware or to fetch assets | |
data, // arbitrary space for passing data between middlewares | |
} = context; | |
const client_id = env.GITHUB_CLIENT_ID; | |
const client_secret = env.GITHUB_CLIENT_SECRET; | |
try { | |
const url = new URL(request.url); | |
const code = url.searchParams.get('code'); | |
const response = await fetch( | |
'https://github.com/login/oauth/access_token', | |
{ | |
method: 'POST', | |
headers: { | |
'content-type': 'application/json', | |
'user-agent': 'cloudflare-functions-github-oauth-login-demo', | |
'accept': 'application/json', | |
}, | |
body: JSON.stringify({ client_id, client_secret, code }), | |
}, | |
); | |
const result = await response.json(); | |
if (result.error) { | |
return new Response(renderBody('error', result), { | |
headers: { | |
'content-type': 'text/html;charset=UTF-8', | |
}, | |
status: 401 | |
}); | |
} | |
const token = result.access_token; | |
const provider = 'github'; | |
const responseBody = renderBody('success', { | |
token, | |
provider, | |
}); | |
return new Response(responseBody, { | |
headers: { | |
'content-type': 'text/html;charset=UTF-8', | |
}, | |
status: 200 | |
}); | |
} catch (error) { | |
console.error(error); | |
return new Response(error.message, { | |
headers: { | |
'content-type': 'text/html;charset=UTF-8', | |
}, | |
status: 500, | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment