@@ -171,39 +171,42 @@ def fetch_user_details(token, id)
171
171
172
172
log ( "user_json:\n #{ user_json . to_yaml } " )
173
173
174
- # Fetch email separately
175
- email_json_url = "https://api.github.com/user/emails"
176
- email_json_response = connection . run_request ( :get , email_json_url , nil , headers )
177
-
178
- log <<-LOG
179
- email_json request: GET #{ email_json_url }
180
-
181
- request headers: #{ headers }
182
-
183
- response status: #{ email_json_response . status }
184
-
185
- response body:
186
- #{ email_json_response . body }
187
- LOG
188
-
189
- if email_json_response . status == 200
190
- email_json = JSON . parse ( email_json_response . body )
191
- primary_email = email_json . find { |e | e [ "primary" ] } &.dig ( "email" )
192
- end
193
174
result = { }
194
175
if user_json . present?
195
176
json_walk ( result , user_json , :user_id )
196
177
json_walk ( result , user_json , :username )
197
178
json_walk ( result , user_json , :name )
198
- result [ :email ] = primary_email if primary_email . present?
179
+ json_walk ( result , user_json , :email )
199
180
json_walk ( result , user_json , :email_verified )
200
181
json_walk ( result , user_json , :avatar )
182
+ end
183
+ # Fetch user emails
184
+ user_emails_response = connection . run_request ( user_json_method , user_emails_url , nil , headers )
201
185
202
- DiscoursePluginRegistry . oauth2_basic_additional_json_paths . each do |detail |
203
- prop = "extra:#{ detail } "
204
- json_walk ( result , user_json , prop , custom_path : detail )
186
+ log <<-LOG
187
+ user_emails request: #{ user_json_method } #{ user_emails_url }
188
+ request headers: #{ headers }
189
+ response status: #{ user_emails_response . status }
190
+ response body:
191
+ #{ user_emails_response . body }
192
+ LOG
193
+
194
+ if user_emails_response . status == 200
195
+ user_emails = JSON . parse ( user_emails_response . body )
196
+ log ( "user_emails:\n #{ user_emails . to_yaml } " )
197
+
198
+ primary_email = user_emails . find { |email | email [ "primary" ] && email [ "verified" ] }
199
+ if primary_email
200
+ result [ :email ] = primary_email [ "email" ]
201
+ else
202
+ # Handle scenario where no primary email is found
203
+ result [ :email ] = nil
205
204
end
205
+ else
206
+ # Handle error fetching emails
207
+ result [ :email ] = nil
206
208
end
209
+
207
210
result
208
211
else
209
212
nil
0 commit comments