Skip to content

Commit 65b4df9

Browse files
authored
fix: update oauth flow to use redirect callback instead of sending messages from popup window (#136)
1 parent 302fe1a commit 65b4df9

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

lib/algora_web/live/onboarding/dev.ex

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ defmodule AlgoraWeb.Onboarding.DevLive do
4949

5050
@impl true
5151
def mount(_params, _session, socket) do
52-
if connected?(socket) do
53-
Phoenix.PubSub.subscribe(Algora.PubSub, "auth:#{socket.id}")
54-
end
55-
5652
context = %{
5753
country: socket.assigns.current_country,
5854
tech_stack: [],
@@ -94,6 +90,35 @@ defmodule AlgoraWeb.Onboarding.DevLive do
9490
|> assign(:signup_form, signup_form)}
9591
end
9692

93+
@impl true
94+
def render(%{current_user: current_user} = assigns) when not is_nil(current_user) do
95+
~H"""
96+
<div
97+
class="w-screen h-screen fixed inset-0 bg-background z-[100]"
98+
phx-hook="LocalStateStore"
99+
id="onboarding-dev-page"
100+
>
101+
<div class="flex items-center justify-center h-full">
102+
<svg
103+
class="mr-3 -ml-1 size-12 animate-spin text-success"
104+
xmlns="http://www.w3.org/2000/svg"
105+
fill="none"
106+
viewBox="0 0 24 24"
107+
>
108+
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4">
109+
</circle>
110+
<path
111+
class="opacity-75"
112+
fill="currentColor"
113+
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
114+
>
115+
</path>
116+
</svg>
117+
</div>
118+
</div>
119+
"""
120+
end
121+
97122
@impl true
98123
def render(assigns) do
99124
~H"""
@@ -127,37 +152,37 @@ defmodule AlgoraWeb.Onboarding.DevLive do
127152
{:noreply, socket}
128153
end
129154

130-
@impl true
131-
def handle_info({:authenticated, user}, socket) do
132-
tech_stack = get_field(socket.assigns.info_form.source, :tech_stack) || []
133-
intentions = get_field(socket.assigns.info_form.source, :intentions) || []
134-
135-
case user
136-
|> change(
137-
tech_stack: tech_stack,
138-
seeking_bounties: "bounties" in intentions,
139-
seeking_contracts: "contracts" in intentions,
140-
seeking_jobs: "jobs" in intentions
141-
)
142-
|> Repo.update() do
143-
{:ok, user} ->
144-
{:noreply, redirect(socket, to: AlgoraWeb.UserAuth.generate_login_path(user.email))}
145-
146-
{:error, changeset} ->
147-
Logger.error("Failed to update user #{user.id} on onboarding: #{inspect(changeset)}")
148-
{:noreply, put_flash(socket, :error, "Something went wrong. Please try again.")}
149-
end
150-
end
151-
152155
@impl true
153156
def handle_event("restore_settings", params, socket) do
154-
{:noreply, LocalStore.restore(socket, params)}
157+
socket = LocalStore.restore(socket, params)
158+
159+
if user = socket.assigns[:current_user] do
160+
tech_stack = get_field(socket.assigns.info_form.source, :tech_stack) || []
161+
intentions = get_field(socket.assigns.info_form.source, :intentions) || []
162+
163+
case user
164+
|> change(
165+
tech_stack: tech_stack,
166+
seeking_bounties: "bounties" in intentions,
167+
seeking_contracts: "contracts" in intentions,
168+
seeking_jobs: "jobs" in intentions
169+
)
170+
|> Repo.update() do
171+
{:ok, user} ->
172+
{:noreply, redirect(socket, to: AlgoraWeb.UserAuth.generate_login_path(user.email))}
173+
174+
{:error, changeset} ->
175+
Logger.error("Failed to update user #{user.id} on onboarding: #{inspect(changeset)}")
176+
{:noreply, put_flash(socket, :error, "Something went wrong. Please try again.")}
177+
end
178+
else
179+
{:noreply, socket}
180+
end
155181
end
156182

157183
@impl true
158184
def handle_event("sign_in_with_github", _params, socket) do
159-
popup_url = Github.authorize_url(%{socket_id: socket.id})
160-
{:noreply, push_event(socket, "open_popup", %{url: popup_url})}
185+
{:noreply, redirect(socket, external: Github.authorize_url(%{return_to: ~p"/onboarding/dev?checkpoint=1"}))}
161186
end
162187

163188
@impl true

lib/algora_web/router.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ defmodule AlgoraWeb.Router do
133133
end
134134

135135
live_session :onboarding,
136-
on_mount: [{AlgoraWeb.Analytics, :current_country}] do
136+
on_mount: [{AlgoraWeb.Analytics, :current_country}, {AlgoraWeb.UserAuth, :current_user}] do
137137
live "/onboarding/org", Onboarding.OrgLive
138138
live "/onboarding/dev", Onboarding.DevLive
139139
live "/community", CommunityLive, :index

0 commit comments

Comments
 (0)