Skip to content

Commit

Permalink
fixes github callback
Browse files Browse the repository at this point in the history
  • Loading branch information
kaxada committed Oct 4, 2024
1 parent 82cb3f1 commit ad93744
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 108 deletions.
108 changes: 0 additions & 108 deletions providers/github/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,114 +85,6 @@ const requestAccessToken = async (code) => {
}
};

const handleOAuthCallback = async (req, res) => {
const code = req.body.code ?? req.query.code;

let issueTitle;
let markdown;

if (req.query.state) {
const encryptedState = req.query.state;
const formData = decrypt(encryptedState);
const parsedFormData = JSON.parse(formData);
issueTitle = parsedFormData.title;
markdown = convertToMarkdown(parsedFormData.body);
}

const { access_token: accessToken, errors: accessTokenErrors } =
await requestAccessToken(code);
if (accessTokenErrors.length > 0) {
res.status(500).send(accessTokenErrors.join());
return;
}

const octokit = new Octokit({ auth: `${accessToken}` });

if (issueTitle && markdown) {
const { data: issue } = await octokit.rest.issues.create({
owner: "badging",
repo: "event-diversity-and-inclusion",
title: issueTitle,
body: markdown,
});

res.redirect(issue.html_url);
return;
}

// Authenticated user details
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(
octokit
);
if (userInfoErrors.length > 0) {
res.status(500).send(userInfoErrors.join());
return;
}

// Save user to database
const savedUser = await saveUser(
userInfo.login,
userInfo.name,
userInfo.email,
userInfo.id,
null
);
if (!savedUser) {
res.status(500).send("Error saving user info");
return;
}

// Public repos they maintain, administer, or own
const { repositories, errors: repositoriesErrors } =
await getUserRepositories(octokit);
if (repositoriesErrors.length > 0) {
res.status(500).send(repositoriesErrors.join());
return;
}

if (process.env.NODE_ENV === "production") {
res.status(200).json({
userId: savedUser.id,
name: savedUser.name,
username: savedUser.login,
email: savedUser.email,
repos: repositories,
provider: "github",
});
} else if (process.env.NODE_ENV === "development") {
res.status(200).send(`
<html>
<head>
<title>Repo List</title>
</head>
<body>
<h1>Welcome ${savedUser.name}</h1>
<h2>Username: ${savedUser.login}</h2>
<h2>Email: ${savedUser.email}</h2>
<form action="/api/repos-to-badge" method="post">
<input type="hidden" name="provider" value="github">
<input type="hidden" name="userId" value="${savedUser.id}">
<h2>Select Repositories:</h2>
${repositories
.map(
(repo) => `
<div>
<input type="checkbox" name="repos[]" value="${repo.id}">
<label for="${repo.id}">${repo.fullName}</label>
</div>
`
)
.join("")}
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`);
} else {
res.status(500).send("Unknown process mode");
}
};

/**
* Sets up the provided Express app routes for GitLab
Expand Down
108 changes: 108 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,114 @@ const setupRoutes = (app) => {
app.get("/api/login", login);

//callbacks
app.get("/api/github/callback", async (req, res) => {
const code = req.body.code ?? req.query.code;

let issueTitle;
let markdown;

if (req.query.state) {
const encryptedState = req.query.state;
const formData = decrypt(encryptedState);
const parsedFormData = JSON.parse(formData);
issueTitle = parsedFormData.title;
markdown = convertToMarkdown(parsedFormData.body);
}

const { access_token: accessToken, errors: accessTokenErrors } =
await requestAccessToken(code);
if (accessTokenErrors.length > 0) {
res.status(500).send(accessTokenErrors.join());
return;
}

const octokit = new Octokit({ auth: `${accessToken}` });

if (issueTitle && markdown) {
const { data: issue } = await octokit.rest.issues.create({
owner: "badging",
repo: "event-diversity-and-inclusion",
title: issueTitle,
body: markdown,
});

res.redirect(issue.html_url);
return;
}

// Authenticated user details
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(
octokit
);
if (userInfoErrors.length > 0) {
res.status(500).send(userInfoErrors.join());
return;
}

// Save user to database
const savedUser = await saveUser(
userInfo.login,
userInfo.name,
userInfo.email,
userInfo.id,
null
);
if (!savedUser) {
res.status(500).send("Error saving user info");
return;
}

// Public repos they maintain, administer, or own
const { repositories, errors: repositoriesErrors } =
await getUserRepositories(octokit);
if (repositoriesErrors.length > 0) {
res.status(500).send(repositoriesErrors.join());
return;
}

if (process.env.NODE_ENV === "production") {
res.status(200).json({
userId: savedUser.id,
name: savedUser.name,
username: savedUser.login,
email: savedUser.email,
repos: repositories,
provider: "github",
});
} else if (process.env.NODE_ENV === "development") {
res.status(200).send(`
<html>
<head>
<title>Repo List</title>
</head>
<body>
<h1>Welcome ${savedUser.name}</h1>
<h2>Username: ${savedUser.login}</h2>
<h2>Email: ${savedUser.email}</h2>
<form action="/api/repos-to-badge" method="post">
<input type="hidden" name="provider" value="github">
<input type="hidden" name="userId" value="${savedUser.id}">
<h2>Select Repositories:</h2>
${repositories
.map(
(repo) => `
<div>
<input type="checkbox" name="repos[]" value="${repo.id}">
<label for="${repo.id}">${repo.fullName}</label>
</div>
`
)
.join("")}
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`);
} else {
res.status(500).send("Unknown process mode");
}
});
githubAuthCallback(app);
gitlabAuthCallback(app);
app.get("/api/badgedRepos", badgedRepos);
Expand Down

0 comments on commit ad93744

Please sign in to comment.