@@ -224,18 +224,9 @@ def link_discord():
224224
225225@auth .route ("/link/github" , methods = ["put" ])
226226def link_github ():
227- # Get an access token
228227 code = request .args .get ("code" )
229-
230- access_token = requests .post (
231- quote (
232- f"https://github.com/login/oauth/access_token?client_id={ config .GitHub .client_id } &client_secret={ config .GitHub .client_secret } &code={ code } "
233- ),
234- headers = {"Accept" : "application/json" },
235- timeout = 180 ,
236- ).json ()
237-
238- access_token = access_token ["access_token" ]
228+ if not code :
229+ return "Code required" , 400
239230
240231 # Get signed-in user
241232 if not request .headers .get ("Authorization" ):
@@ -249,6 +240,14 @@ def link_github():
249240 elif usr == 33 :
250241 return "Token Expired" , 401
251242
243+ access_token = requests .post (
244+ quote (
245+ f"https://github.com/login/oauth/access_token?client_id={ config .GitHub .client_id } &client_secret={ config .GitHub .client_secret } &code={ code } "
246+ ),
247+ headers = {"Accept" : "application/json" },
248+ timeout = 180 ,
249+ ).json ()["access_token" ]
250+
252251 # Get github ID
253252 github = requests .get (
254253 "https://api.github.com/user" ,
@@ -257,6 +256,18 @@ def link_github():
257256 ).json ()
258257
259258 conn = util .make_connection ()
259+
260+ existing_user = util .exec_query (
261+ conn ,
262+ "select from users where github_id = :g_id;" ,
263+ g_id = github ["id" ],
264+ id = usr .id ,
265+ ).one_or_none ()
266+
267+ # the actual proposed solution is too awkward :sob:
268+ if existing_user is not None :
269+ return "A user with that GitHub ID already exists! Contact an admin to solve this issue!" , 409
270+
260271 try :
261272 util .exec_query (
262273 conn ,
@@ -270,4 +281,4 @@ def link_github():
270281 return "Something went wrong!" , 500
271282 conn .commit ()
272283
273- return "Discord linked!" , 200
284+ return "GitHub linked!" , 200
0 commit comments