@@ -136,11 +136,7 @@ defmodule Ueberauth.Strategy.Github do
136136 Fetches the uid field from the Github response. This defaults to the option `uid_field` which in-turn defaults to `id`
137137 """
138138 def uid ( conn ) do
139- user =
140- conn
141- |> option ( :uid_field )
142- |> to_string
143- conn . private . github_user [ user ]
139+ conn |> option ( :uid_field ) |> to_string ( ) |> fetch_uid ( conn )
144140 end
145141
146142 @ doc """
@@ -170,7 +166,7 @@ defmodule Ueberauth.Strategy.Github do
170166 % Info {
171167 name: user [ "name" ] ,
172168 nickname: user [ "login" ] ,
173- email: user [ "email" ] || Enum . find ( user [ "emails" ] || [ ] , & ( & 1 [ "primary" ] ) ) [ "email" ] ,
169+ email: fetch_email! ( user ) ,
174170 location: user [ "location" ] ,
175171 image: user [ "avatar_url" ] ,
176172 urls: % {
@@ -203,6 +199,27 @@ defmodule Ueberauth.Strategy.Github do
203199 }
204200 end
205201
202+ defp fetch_uid ( "email" , % { private: % { github_user: user } } ) do
203+ # private email will not be available as :email and must be fetched
204+ fetch_email! ( user )
205+ end
206+
207+ defp fetch_uid ( field , conn ) do
208+ conn . private . github_user [ field ]
209+ end
210+
211+ defp fetch_email! ( user ) do
212+ user [ "email" ] || get_primary_email! ( user )
213+ end
214+
215+ defp get_primary_email! ( user ) do
216+ unless user [ "emails" ] && ( Enum . count ( user [ "emails" ] ) > 0 ) do
217+ raise "Unable to access the user's email address"
218+ end
219+
220+ Enum . find ( user [ "emails" ] , & ( & 1 [ "primary" ] ) ) [ "email" ]
221+ end
222+
206223 defp fetch_user ( conn , token ) do
207224 conn = put_private ( conn , :github_token , token )
208225 # Will be better with Elixir 1.3 with/else
0 commit comments