Skip to content

Commit a29735d

Browse files
committed
feat: add bounty and tip forms on homepage (#26)
1 parent b90080c commit a29735d

File tree

7 files changed

+302
-37
lines changed

7 files changed

+302
-37
lines changed

assets/js/app.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,23 @@ window.addEventListener("phx:js-exec", ({ detail }) => {
623623
liveSocket.execJS(el, el.getAttribute(detail.attr));
624624
});
625625
});
626+
627+
window.addEventListener("phx:open_popup", (e: CustomEvent) => {
628+
const url = e.detail.url;
629+
if (!url) return;
630+
631+
const width = e.detail.width || 600;
632+
const height = e.detail.height || 600;
633+
const left = e.detail.left || window.screen.width / 2 - width / 2;
634+
const top = e.detail.top || window.screen.height / 2 - height / 2;
635+
636+
const newWindow = window.open(
637+
url,
638+
"oauth",
639+
`width=${width},height=${height},left=${left},top=${top},toolbar=0,scrollbars=1,status=1`
640+
);
641+
642+
if (window.focus && newWindow) {
643+
newWindow.focus();
644+
}
645+
});

lib/algora_web/controllers/oauth_callback_controller.ex

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,54 @@ defmodule AlgoraWeb.OAuthCallbackController do
1414
end
1515
end
1616

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+
1722
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)
2724

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
3630

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)
4132

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
4440

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)
4846

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
4954
{:error, reason} ->
5055
Logger.debug("failed GitHub exchange #{inspect(reason)}")
56+
conn = put_flash(conn, :error, translate_error(reason))
5157

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
5565
end
5666
end
5767

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule AlgoraWeb.OAuthCallbackHTML do
2+
use AlgoraWeb, :html
3+
4+
embed_templates "oauth_callback_html/*"
5+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="text-center p-8">
2+
<h2 class="text-xl font-semibold text-destructive">Authentication Failed</h2>
3+
<p>Please try again.</p>
4+
</div>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
window.close();
3+
</script>
4+
<div class="text-center p-8">
5+
<h2 class="text-xl font-semibold text-success">Authentication Successful!</h2>
6+
<p>You can close this window.</p>
7+
</div>

lib/algora_web/controllers/user_auth.ex

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,20 @@ defmodule AlgoraWeb.UserAuth do
8686
"""
8787
def log_in_user(conn, user) do
8888
user_return_to = get_session(conn, :user_return_to)
89-
conn = assign(conn, :current_user, user)
9089

91-
conn =
92-
conn
93-
|> renew_session()
94-
|> put_session(:user_id, user.id)
95-
|> put_session(:last_context, user.last_context)
96-
|> put_session(:live_socket_id, "users_sessions:#{user.id}")
90+
conn
91+
|> put_current_user(user)
92+
|> redirect(to: user_return_to || signed_in_path(conn))
93+
end
9794

98-
redirect(conn, to: user_return_to || signed_in_path(conn))
95+
def put_current_user(conn, user) do
96+
conn = assign(conn, :current_user, user)
97+
98+
conn
99+
|> renew_session()
100+
|> put_session(:user_id, user.id)
101+
|> put_session(:last_context, user.last_context)
102+
|> put_session(:live_socket_id, "users_sessions:#{user.id}")
99103
end
100104

101105
defp renew_session(conn) do

0 commit comments

Comments
 (0)