@@ -85,6 +85,114 @@ 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+ } ;
88196
89197module . exports = {
90198 githubAuth,
0 commit comments