Skip to content

Commit c7f36c5

Browse files
committed
fix: bring up oauth popup before app installation
1 parent 40cfabe commit c7f36c5

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

lib/algora_web/live/org/settings_live.ex

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule AlgoraWeb.Org.SettingsLive do
44

55
alias Algora.Accounts
66
alias Algora.Accounts.User
7+
alias Algora.Github
78
alias AlgoraWeb.Components.Logos
89

910
def render(assigns) do
@@ -36,19 +37,15 @@ defmodule AlgoraWeb.Org.SettingsLive do
3637
</div>
3738
</div>
3839
<% end %>
39-
<.link href={Algora.Github.install_url()} rel="noopener" class="ml-auto gap-2">
40-
<.button>
41-
<Logos.github class="w-4 h-4 mr-2 -ml-1" />
42-
Manage {ngettext("installation", "installations", length(@installations))}
43-
</.button>
44-
</.link>
40+
<.button phx-click="install_app" class="ml-auto gap-2">
41+
<Logos.github class="w-4 h-4 mr-2 -ml-1" />
42+
Manage {ngettext("installation", "installations", length(@installations))}
43+
</.button>
4544
<% else %>
4645
<div class="flex flex-col gap-2">
47-
<.link href={Algora.Github.install_url()} rel="noopener" class="ml-auto gap-2">
48-
<.button>
49-
<Logos.github class="w-4 h-4 mr-2 -ml-1" /> Install GitHub App
50-
</.button>
51-
</.link>
46+
<.button phx-click="install_app" class="ml-auto gap-2">
47+
<Logos.github class="w-4 h-4 mr-2 -ml-1" /> Install GitHub App
48+
</.button>
5249
</div>
5350
<% end %>
5451
</div>
@@ -102,6 +99,10 @@ defmodule AlgoraWeb.Org.SettingsLive do
10299
end
103100

104101
def mount(_params, _session, socket) do
102+
if connected?(socket) do
103+
Phoenix.PubSub.subscribe(Algora.PubSub, "auth:#{socket.id}")
104+
end
105+
105106
%{current_org: current_org} = socket.assigns
106107

107108
changeset = User.settings_changeset(current_org, %{})
@@ -110,9 +111,20 @@ defmodule AlgoraWeb.Org.SettingsLive do
110111
{:ok,
111112
socket
112113
|> assign(:installations, installations)
114+
|> assign(:oauth_url, Github.authorize_url(%{socket_id: socket.id}))
113115
|> assign_form(changeset)}
114116
end
115117

118+
def handle_info({:authenticated, user}, socket) do
119+
# TODO: skip auth step in installation flow
120+
{:noreply, socket |> assign(:current_user, user) |> redirect(external: Github.install_url())}
121+
end
122+
123+
def handle_event("install_app", _params, socket) do
124+
# TODO: immediately redirect to install_url if user has valid token
125+
{:noreply, push_event(socket, "open_popup", %{url: socket.assigns.oauth_url})}
126+
end
127+
116128
def handle_event("validate", %{"user" => params}, socket) do
117129
changeset =
118130
socket.assigns.current_org

0 commit comments

Comments
 (0)