1
+ import type FetchError from 'ofetch' ;
1
2
2
3
export default defineOAuthGitHubEventHandler ( {
3
4
config : {
4
5
} ,
5
6
async onSuccess ( event , { user, tokens } ) {
7
+ const config = useRuntimeConfig ( event ) ;
8
+ const logger = console ;
9
+
6
10
await setUserSession ( event , {
7
11
user : {
8
12
githubId : user . id ,
@@ -15,6 +19,38 @@ export default defineOAuthGitHubEventHandler({
15
19
}
16
20
}
17
21
)
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
+
18
54
return sendRedirect ( event , '/' )
19
55
} ,
20
56
// Optional, will return a json error and 401 status code by default
0 commit comments