@@ -14,44 +14,54 @@ defmodule AlgoraWeb.OAuthCallbackController do
14
14
end
15
15
end
16
16
17
+ def translate_error ( :invalid ) , do: "Unable to verify your login request. Please try signing in again"
18
+ def translate_error ( :expired ) , do: "Your login link has expired. Please request a new one to continue"
19
+ def translate_error ( % Ecto.Changeset { } ) , do: "We were unable to fetch the necessary information from your GitHub account"
20
+ def translate_error ( _reason ) , do: "We were unable to contact GitHub. Please try again later"
21
+
17
22
def new ( conn , % { "provider" => "github" , "code" => code , "state" => state } ) do
18
- with { :ok , data } <- Github . verify_oauth_state ( state ) ,
19
- { :ok , info } <- Github.OAuth . exchange_access_token ( code: code , state: state ) ,
20
- % { info: info , primary_email: primary , emails: emails , token: token } = info ,
21
- { :ok , user } <- Accounts . register_github_user ( primary , info , emails , token ) do
22
- conn =
23
- case data [ :return_to ] do
24
- nil -> conn
25
- return_to -> put_session ( conn , :user_return_to , return_to )
26
- end
23
+ res = Github . verify_oauth_state ( state )
27
24
28
- conn
29
- |> put_flash ( :info , welcome_message ( user ) )
30
- |> AlgoraWeb.UserAuth . log_in_user ( user )
31
- else
32
- { :error , :invalid } ->
33
- conn
34
- |> put_flash ( :error , "Unable to verify your login request. Please try signing in again." )
35
- |> redirect ( to: "/" )
25
+ socket_id =
26
+ case res do
27
+ { :ok , % { socket_id: socket_id } } -> socket_id
28
+ _ -> nil
29
+ end
36
30
37
- { :error , :expired } ->
38
- conn
39
- |> put_flash ( :error , "Your login link has expired. Please request a new one to continue." )
40
- |> redirect ( to: "/" )
31
+ type = if ( socket_id , do: :popup , else: :redirect )
41
32
42
- { :error , % Ecto.Changeset { } = changeset } ->
43
- Logger . debug ( "failed GitHub insert #{ inspect ( changeset . errors ) } " )
33
+ with { :ok , data } <- res ,
34
+ { :ok , info } <- Github.OAuth . exchange_access_token ( code: code , state: state ) ,
35
+ % { info: info , primary_email: primary , emails: emails , token: token } = info ,
36
+ { :ok , user } <- Accounts . register_github_user ( primary , info , emails , token ) do
37
+ if socket_id do
38
+ Phoenix.PubSub . broadcast ( Algora.PubSub , "auth:#{ socket_id } " , { :authenticated , user } )
39
+ end
44
40
45
- conn
46
- |> put_flash ( :error , "We were unable to fetch the necessary information from your GitHub account" )
47
- |> redirect ( to: "/" )
41
+ case type do
42
+ :popup ->
43
+ conn
44
+ |> AlgoraWeb.UserAuth . put_current_user ( user )
45
+ |> render ( :success )
48
46
47
+ :redirect ->
48
+ conn
49
+ |> put_flash ( :info , welcome_message ( user ) )
50
+ |> AlgoraWeb.UserAuth . put_current_user ( user )
51
+ |> redirect ( to: data [ :return_to ] || AlgoraWeb.UserAuth . signed_in_path ( conn ) )
52
+ end
53
+ else
49
54
{ :error , reason } ->
50
55
Logger . debug ( "failed GitHub exchange #{ inspect ( reason ) } " )
56
+ conn = put_flash ( conn , :error , translate_error ( reason ) )
51
57
52
- conn
53
- |> put_flash ( :error , "We were unable to contact GitHub. Please try again later" )
54
- |> redirect ( to: "/" )
58
+ case type do
59
+ :popup ->
60
+ render ( conn , :error )
61
+
62
+ :redirect ->
63
+ redirect ( conn , to: "/" )
64
+ end
55
65
end
56
66
end
57
67
0 commit comments