Skip to content

Commit 937d22e

Browse files
committed
feat: add OAuth check for managing installations on org settings
1 parent 33054df commit 937d22e

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

lib/algora_web/live/org/settings_live.ex

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

1111
require Logger
1212

13+
@impl true
1314
def render(assigns) do
1415
~H"""
1516
<div class="container mx-auto max-w-7xl space-y-6 p-6">
@@ -137,6 +138,7 @@ defmodule AlgoraWeb.Org.SettingsLive do
137138
"""
138139
end
139140

141+
@impl true
140142
def mount(_params, _session, socket) do
141143
if connected?(socket) do
142144
Phoenix.PubSub.subscribe(Algora.PubSub, "auth:#{socket.id}")
@@ -150,25 +152,48 @@ defmodule AlgoraWeb.Org.SettingsLive do
150152

151153
{:ok,
152154
socket
155+
|> assign(:has_fresh_token?, Accounts.has_fresh_token?(socket.assigns.current_user))
153156
|> assign(:installations, installations)
154157
|> assign(:oauth_url, Github.authorize_url(%{socket_id: socket.id}))
155158
|> assign_has_default_payment_method()
156159
|> assign_form(changeset)}
157160
end
158161

162+
@impl true
159163
def handle_info({:authenticated, user}, socket) do
160-
{:noreply, socket |> assign(:current_user, user) |> redirect(external: Github.install_url_select_target())}
164+
socket =
165+
socket
166+
|> assign(:current_user, user)
167+
|> assign(:has_fresh_token?, true)
168+
169+
case socket.assigns.pending_action do
170+
{event, params} ->
171+
socket = assign(socket, :pending_action, nil)
172+
handle_event(event, params, socket)
173+
174+
nil ->
175+
{:noreply, socket}
176+
end
161177
end
162178

179+
@impl true
163180
def handle_info(:payments_updated, socket) do
164181
{:noreply, assign_has_default_payment_method(socket)}
165182
end
166183

167-
def handle_event("install_app", _params, socket) do
168-
# TODO: immediately redirect to install_url if user has valid token
169-
{:noreply, push_event(socket, "open_popup", %{url: socket.assigns.oauth_url})}
184+
@impl true
185+
def handle_event("install_app" = event, unsigned_params, socket) do
186+
{:noreply,
187+
if socket.assigns.has_fresh_token? do
188+
redirect(socket, external: Github.install_url_select_target())
189+
else
190+
socket
191+
|> assign(:pending_action, {event, unsigned_params})
192+
|> push_event("open_popup", %{url: socket.assigns.oauth_url})
193+
end}
170194
end
171195

196+
@impl true
172197
def handle_event("validate", %{"user" => params}, socket) do
173198
changeset =
174199
socket.assigns.current_org
@@ -178,6 +203,7 @@ defmodule AlgoraWeb.Org.SettingsLive do
178203
{:noreply, assign_form(socket, changeset)}
179204
end
180205

206+
@impl true
181207
def handle_event("save", %{"user" => params}, socket) do
182208
case Accounts.update_settings(socket.assigns.current_org, params) do
183209
{:ok, user} ->
@@ -191,6 +217,7 @@ defmodule AlgoraWeb.Org.SettingsLive do
191217
end
192218
end
193219

220+
@impl true
194221
def handle_event("setup_payment", _params, socket) do
195222
%{current_org: org} = socket.assigns
196223
success_url = url(~p"/org/#{org.handle}/settings")
@@ -206,6 +233,7 @@ defmodule AlgoraWeb.Org.SettingsLive do
206233
end
207234
end
208235

236+
@impl true
209237
def handle_params(params, _url, socket) do
210238
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
211239
end

0 commit comments

Comments
 (0)