add gh app
This commit is contained in:
27
functions/api/api.ts
Normal file
27
functions/api/api.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
export async function onRequest(context: any) {
|
||||
const {
|
||||
request, // same as existing Worker API
|
||||
env, // same as existing Worker API
|
||||
} = context;
|
||||
|
||||
const client_id = (env as any).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: any) {
|
||||
console.error(error);
|
||||
return new Response(error.message, {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user