Skip to content

Commit

Permalink
Merge pull request #23 from fullstackedorg/iso-git
Browse files Browse the repository at this point in the history
Add Git
  • Loading branch information
cplepage authored Mar 12, 2024
2 parents 09bd603 + afb1aa7 commit 28860fa
Show file tree
Hide file tree
Showing 49 changed files with 3,339 additions and 90 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion editor/api/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { fs as globalFS } from "../../../src/api/fs";
import projects from "../projects";

import { Project } from "../projects/types";
import { Project, GitAuths } from "../projects/types";
import { CONFIG_TYPE } from "./types";

declare var fs: typeof globalFS;
Expand All @@ -11,6 +11,7 @@ export const configdir = ".config/fullstacked";

type DATA_TYPE = {
[CONFIG_TYPE.PROJECTS]: Project[];
[CONFIG_TYPE.GIT]: GitAuths;
};

export default {
Expand Down
3 changes: 2 additions & 1 deletion editor/api/config/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export enum CONFIG_TYPE {
PROJECTS = "projects"
PROJECTS = "projects",
GIT = "git"
}
80 changes: 80 additions & 0 deletions editor/api/git/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { auth } from ".";
import type { fetch as globalFetch } from "../../../src/api/fetch";

declare var fetch: typeof globalFetch;

const client_id = "175231928f47d8d36b2d";

export default {
async deviceFlowStart() {
const response = await fetch("https://github.com/login/device/code", {
method: "POST",
headers: {
"content-type": "application/json",
accept: "application/json"
},
body: JSON.stringify({
client_id,
scope: "repo,user:email"
}),
encoding: "utf8"
});
return response.body as string;
},
async deviceFlowPoll(device_code: string) {
const response = await fetch(
"https://github.com/login/oauth/access_token",
{
method: "POST",
headers: {
"content-type": "application/json",
accept: "application/json"
},
body: JSON.stringify({
client_id,
device_code,
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
}),
encoding: "utf8"
}
);

const json = JSON.parse(response.body as string);

if (json.error === "slow_down") return { wait: json.interval };
else if (json.error === "authorization_pending") return { wait: 5 };

if (!json.access_token) return { error: "Failed" };

const { access_token } = json;

const userResponse = await fetch("https://api.github.com/user", {
headers: {
authorization: `Bearer ${access_token}`,
accept: "application/json"
},
encoding: "utf8"
});

const user = JSON.parse(userResponse.body as string);

const username = user.login;

const emailsResponse = await fetch(
"https://api.github.com/user/emails",
{
headers: {
authorization: `Bearer ${access_token}`,
accept: "application/json"
},
encoding: "utf8"
}
);

const emails = JSON.parse(emailsResponse.body as string);

const email = emails?.find((emailEntry) => emailEntry?.primary)?.email;

await auth("github.com", username, email, access_token);
}
};
Loading

0 comments on commit 28860fa

Please sign in to comment.