Skip to content

Commit ad93744

Browse files
committed
fixes github callback
1 parent 82cb3f1 commit ad93744

File tree

2 files changed

+108
-108
lines changed

2 files changed

+108
-108
lines changed

providers/github/auth.js

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -85,114 +85,6 @@ const requestAccessToken = async (code) => {
8585
}
8686
};
8787

88-
const handleOAuthCallback = async (req, res) => {
89-
const code = req.body.code ?? req.query.code;
90-
91-
let issueTitle;
92-
let markdown;
93-
94-
if (req.query.state) {
95-
const encryptedState = req.query.state;
96-
const formData = decrypt(encryptedState);
97-
const parsedFormData = JSON.parse(formData);
98-
issueTitle = parsedFormData.title;
99-
markdown = convertToMarkdown(parsedFormData.body);
100-
}
101-
102-
const { access_token: accessToken, errors: accessTokenErrors } =
103-
await requestAccessToken(code);
104-
if (accessTokenErrors.length > 0) {
105-
res.status(500).send(accessTokenErrors.join());
106-
return;
107-
}
108-
109-
const octokit = new Octokit({ auth: `${accessToken}` });
110-
111-
if (issueTitle && markdown) {
112-
const { data: issue } = await octokit.rest.issues.create({
113-
owner: "badging",
114-
repo: "event-diversity-and-inclusion",
115-
title: issueTitle,
116-
body: markdown,
117-
});
118-
119-
res.redirect(issue.html_url);
120-
return;
121-
}
122-
123-
// Authenticated user details
124-
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(
125-
octokit
126-
);
127-
if (userInfoErrors.length > 0) {
128-
res.status(500).send(userInfoErrors.join());
129-
return;
130-
}
131-
132-
// Save user to database
133-
const savedUser = await saveUser(
134-
userInfo.login,
135-
userInfo.name,
136-
userInfo.email,
137-
userInfo.id,
138-
null
139-
);
140-
if (!savedUser) {
141-
res.status(500).send("Error saving user info");
142-
return;
143-
}
144-
145-
// Public repos they maintain, administer, or own
146-
const { repositories, errors: repositoriesErrors } =
147-
await getUserRepositories(octokit);
148-
if (repositoriesErrors.length > 0) {
149-
res.status(500).send(repositoriesErrors.join());
150-
return;
151-
}
152-
153-
if (process.env.NODE_ENV === "production") {
154-
res.status(200).json({
155-
userId: savedUser.id,
156-
name: savedUser.name,
157-
username: savedUser.login,
158-
email: savedUser.email,
159-
repos: repositories,
160-
provider: "github",
161-
});
162-
} else if (process.env.NODE_ENV === "development") {
163-
res.status(200).send(`
164-
<html>
165-
<head>
166-
<title>Repo List</title>
167-
</head>
168-
<body>
169-
<h1>Welcome ${savedUser.name}</h1>
170-
<h2>Username: ${savedUser.login}</h2>
171-
<h2>Email: ${savedUser.email}</h2>
172-
<form action="/api/repos-to-badge" method="post">
173-
<input type="hidden" name="provider" value="github">
174-
<input type="hidden" name="userId" value="${savedUser.id}">
175-
<h2>Select Repositories:</h2>
176-
${repositories
177-
.map(
178-
(repo) => `
179-
<div>
180-
<input type="checkbox" name="repos[]" value="${repo.id}">
181-
<label for="${repo.id}">${repo.fullName}</label>
182-
</div>
183-
`
184-
)
185-
.join("")}
186-
<br>
187-
<input type="submit" value="Submit">
188-
</form>
189-
</body>
190-
</html>
191-
`);
192-
} else {
193-
res.status(500).send("Unknown process mode");
194-
}
195-
};
19688

19789
/**
19890
* Sets up the provided Express app routes for GitLab

routes/index.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,114 @@ const setupRoutes = (app) => {
160160
app.get("/api/login", login);
161161

162162
//callbacks
163+
app.get("/api/github/callback", async (req, res) => {
164+
const code = req.body.code ?? req.query.code;
165+
166+
let issueTitle;
167+
let markdown;
168+
169+
if (req.query.state) {
170+
const encryptedState = req.query.state;
171+
const formData = decrypt(encryptedState);
172+
const parsedFormData = JSON.parse(formData);
173+
issueTitle = parsedFormData.title;
174+
markdown = convertToMarkdown(parsedFormData.body);
175+
}
176+
177+
const { access_token: accessToken, errors: accessTokenErrors } =
178+
await requestAccessToken(code);
179+
if (accessTokenErrors.length > 0) {
180+
res.status(500).send(accessTokenErrors.join());
181+
return;
182+
}
183+
184+
const octokit = new Octokit({ auth: `${accessToken}` });
185+
186+
if (issueTitle && markdown) {
187+
const { data: issue } = await octokit.rest.issues.create({
188+
owner: "badging",
189+
repo: "event-diversity-and-inclusion",
190+
title: issueTitle,
191+
body: markdown,
192+
});
193+
194+
res.redirect(issue.html_url);
195+
return;
196+
}
197+
198+
// Authenticated user details
199+
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(
200+
octokit
201+
);
202+
if (userInfoErrors.length > 0) {
203+
res.status(500).send(userInfoErrors.join());
204+
return;
205+
}
206+
207+
// Save user to database
208+
const savedUser = await saveUser(
209+
userInfo.login,
210+
userInfo.name,
211+
userInfo.email,
212+
userInfo.id,
213+
null
214+
);
215+
if (!savedUser) {
216+
res.status(500).send("Error saving user info");
217+
return;
218+
}
219+
220+
// Public repos they maintain, administer, or own
221+
const { repositories, errors: repositoriesErrors } =
222+
await getUserRepositories(octokit);
223+
if (repositoriesErrors.length > 0) {
224+
res.status(500).send(repositoriesErrors.join());
225+
return;
226+
}
227+
228+
if (process.env.NODE_ENV === "production") {
229+
res.status(200).json({
230+
userId: savedUser.id,
231+
name: savedUser.name,
232+
username: savedUser.login,
233+
email: savedUser.email,
234+
repos: repositories,
235+
provider: "github",
236+
});
237+
} else if (process.env.NODE_ENV === "development") {
238+
res.status(200).send(`
239+
<html>
240+
<head>
241+
<title>Repo List</title>
242+
</head>
243+
<body>
244+
<h1>Welcome ${savedUser.name}</h1>
245+
<h2>Username: ${savedUser.login}</h2>
246+
<h2>Email: ${savedUser.email}</h2>
247+
<form action="/api/repos-to-badge" method="post">
248+
<input type="hidden" name="provider" value="github">
249+
<input type="hidden" name="userId" value="${savedUser.id}">
250+
<h2>Select Repositories:</h2>
251+
${repositories
252+
.map(
253+
(repo) => `
254+
<div>
255+
<input type="checkbox" name="repos[]" value="${repo.id}">
256+
<label for="${repo.id}">${repo.fullName}</label>
257+
</div>
258+
`
259+
)
260+
.join("")}
261+
<br>
262+
<input type="submit" value="Submit">
263+
</form>
264+
</body>
265+
</html>
266+
`);
267+
} else {
268+
res.status(500).send("Unknown process mode");
269+
}
270+
});
163271
githubAuthCallback(app);
164272
gitlabAuthCallback(app);
165273
app.get("/api/badgedRepos", badgedRepos);

0 commit comments

Comments
 (0)