Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mudloop committed Nov 8, 2024
1 parent 414160a commit 4be37ee
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ out

# Local Netlify folder
.netlify


local.config.json
Binary file modified backend/bun.lockb
Binary file not shown.
32 changes: 24 additions & 8 deletions backend/netlify/functions/github-auth.mts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import type { Context } from "@netlify/functions";
import { existsSync, readFileSync } from "fs";
import { resolve } from 'path';

const client_id = Netlify.env.get('GITHUB_CLIENT_ID');
const client_secret = Netlify.env.get('GITHUB_CLIENT_SECRET');
const redirectUrl = Netlify.env.get('GITHUB_REDIRECT_URL');
const readLocalConfig = (path: string) => {
console.log(path);
return existsSync(path) ? JSON.parse(readFileSync(path)) : undefined;
}

export default async (req: Request, _context: Context) => {
const code = new URL(req.url).searchParams.get('code');

if (!code) {
return new Response("Missing authorization code", { status: 400 });
const config = Netlify.env.has('GITHUB_CLIENT_ID') ? {
client_id: Netlify.env.get('GITHUB_CLIENT_ID'),
client_secret: Netlify.env.get('GITHUB_CLIENT_SECRET'),
redirectUrl: Netlify.env.get('GITHUB_REDIRECT_URL')
} : readLocalConfig(resolve('local.config.json'));
if (!config) {
return new Response(JSON.stringify({
error: 'Invalid config'
}), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
const { client_id, client_secret, redirectUrl } = config;
console.log(client_id, client_secret, code);

if (!code) return new Response("Missing authorization code", { status: 400 });

const response = await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
Expand All @@ -31,7 +47,6 @@ export default async (req: Request, _context: Context) => {
const access_token = data.access_token;

if (access_token) {
// Redirect to the frontend with the token as a URL parameter, or handle it as preferred
const redirectWithToken = `${redirectUrl}?access_token=${access_token}`;
return new Response(null, {
status: 302,
Expand All @@ -40,4 +55,5 @@ export default async (req: Request, _context: Context) => {
} else {
return new Response("Failed to retrieve access token", { status: 500 });
}
};
};

2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "dependencies": { "@netlify/functions": "^2.8.2" } }
{ "dependencies": { "@netlify/functions": "^2.8.2", "fs": "^0.0.1-security", "path": "^0.12.7" } }
27 changes: 27 additions & 0 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"noEmitOnError": true,
"lib": [ "es2021", "dom", "DOM.Iterable" ],
"strict": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"importHelpers": true,
"outDir": "./dist",
"sourceMap": true,
"inlineSources": true,
"rootDir": "./netlify/functions",
"declaration": true,
"incremental": true,
"skipLibCheck": true,
"useDefineForClassFields": false,
"resolveJsonModule": true,
"paths": {
"*": [ "./node_modules/*" ]
}
},
"include": [ "./netlify/functions/**/*.ts" ]
}
10 changes: 7 additions & 3 deletions docs/bundle/js/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/bundle/js/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/bundle/js/monaco.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions docs/bundle/js/monaco.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions playground/assets/icons/github.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions playground/src/components/Sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { Playground } from "./Playground";
import { COMMON_STYLES } from './common-styles';

@customElement('cmaj-sidebar') export class Sidebar extends LitElement {
githubAuth(e: any) {
window.open('https://github.com/login/oauth/authorize?client_id=Ov23li52ClmsCJFfVhqc', '_blank');
}
@property({ type: Boolean }) hideProjectPanel = false;
@property({ type: Object }) playground!: Playground;
static styles = css`
Expand Down Expand Up @@ -73,6 +76,10 @@ import { COMMON_STYLES } from './common-styles';
: html`<div class="logo"><img src="${new URL(logo, import.meta.url)}"><span>BETA</span></div>`}
<slot name="close"></slot>
</div>
<button @click=${e => this.githubAuth(e)}>
<ui-icon currentColors icon="github"></ui-icon>
Connect
</button>
${this.hideProjectPanel
? html`${this.playground.project?.modified ? html`<button @click=${() => this.playground.resetProject()}>Reset</button>` : ''}`
: html`<cmaj-projects .playground=${this.playground}></cmaj-projects>`}
Expand Down

0 comments on commit 4be37ee

Please sign in to comment.