Skip to content

Commit efe93d0

Browse files
authored
support for public gh app (#165)
1 parent 71bc8ec commit efe93d0

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

nuxt.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ export default defineNuxtConfig({
6565
github: {
6666
enabled: true,
6767
clientId: '',
68-
clientSecret: '',
69-
scope: ''
68+
clientSecret: ''
7069
}
7170
},
7271
nitro: {
@@ -84,7 +83,7 @@ export default defineNuxtConfig({
8483
oauth: {
8584
github: {
8685
clientId: '',
87-
clientSecret: '',
86+
clientSecret: ''
8887
}
8988
},
9089
public: {
@@ -94,7 +93,8 @@ export default defineNuxtConfig({
9493
githubEnt: '',
9594
githubTeam: '',
9695
usingGithubAuth: false,
97-
version
96+
version,
97+
isPublicApp: false
9898
}
9999
}
100100
})

server/routes/auth/github.get.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import type FetchError from 'ofetch';
12

23
export default defineOAuthGitHubEventHandler({
34
config: {
45
},
56
async onSuccess(event, { user, tokens }) {
7+
const config = useRuntimeConfig(event);
8+
const logger = console;
9+
610
await setUserSession(event, {
711
user: {
812
githubId: user.id,
@@ -15,6 +19,38 @@ export default defineOAuthGitHubEventHandler({
1519
}
1620
}
1721
)
22+
23+
// need to check if this is public app (no default org/team/ent)
24+
if (config.public.isPublicApp) {
25+
try {
26+
const installationsResponse = await $fetch('https://api.github.com/user/installations', {
27+
headers: {
28+
Authorization: `token ${tokens.access_token}`,
29+
Accept: 'application/vnd.github+json',
30+
'X-GitHub-Api-Version': '2022-11-28'
31+
}
32+
}) as { installations: Array<{ account: { login: string } }> };
33+
34+
const installations = installationsResponse.installations;
35+
const organizations = installations.map(installation => installation.account.login);
36+
37+
await setUserSession(event, {
38+
organizations
39+
});
40+
logger.info('User organizations:', organizations);
41+
42+
if (organizations.length === 0) {
43+
console.error('No organizations found for the user.');
44+
return sendRedirect(event, '/?error=No organizations found for the user.');
45+
}
46+
47+
return sendRedirect(event, `/orgs/${organizations[0]}`);
48+
}
49+
catch (error: FetchError) {
50+
logger.error('Error fetching installations:', error);
51+
}
52+
}
53+
1854
return sendRedirect(event, '/')
1955
},
2056
// Optional, will return a json error and 401 status code by default

0 commit comments

Comments
 (0)