From e90eb624806d61a8680957665329e11f238c8201 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Fri, 27 Sep 2024 23:41:10 +0530 Subject: [PATCH 1/8] fix: return to original page after login (#101) Signed-off-by: bryans-go --- lib/algora_web/controllers/oauth_login_controller.ex | 5 ++++- lib/algora_web/controllers/redirect_controller.ex | 6 ++++-- lib/algora_web/controllers/user_auth.ex | 10 +++++++--- lib/algora_web/live/show_live/show.ex | 6 +++++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/algora_web/controllers/oauth_login_controller.ex b/lib/algora_web/controllers/oauth_login_controller.ex index 20cb59ba..0ac7932c 100644 --- a/lib/algora_web/controllers/oauth_login_controller.ex +++ b/lib/algora_web/controllers/oauth_login_controller.ex @@ -1,6 +1,7 @@ defmodule AlgoraWeb.OAuthLoginController do use AlgoraWeb, :controller require Logger + import AlgoraWeb.UserAuth, only: [maybe_store_return_to: 1] def new(conn, %{"provider" => "restream"} = params) do if conn.assigns.current_user do @@ -11,7 +12,9 @@ defmodule AlgoraWeb.OAuthLoginController do |> put_session(:restream_state, state) |> redirect(external: Algora.Restream.authorize_url(state)) else - conn |> redirect(to: ~p"/auth/login") + conn + |> maybe_store_return_to() + |> redirect(to: ~p"/auth/login") end end end diff --git a/lib/algora_web/controllers/redirect_controller.ex b/lib/algora_web/controllers/redirect_controller.ex index 264befd8..2573a709 100644 --- a/lib/algora_web/controllers/redirect_controller.ex +++ b/lib/algora_web/controllers/redirect_controller.ex @@ -1,7 +1,7 @@ defmodule AlgoraWeb.RedirectController do use AlgoraWeb, :controller - import AlgoraWeb.UserAuth, only: [fetch_current_user: 2] + import AlgoraWeb.UserAuth, only: [fetch_current_user: 2, maybe_store_return_to: 1] plug :fetch_current_user @@ -9,7 +9,9 @@ defmodule AlgoraWeb.RedirectController do if conn.assigns.current_user do AlgoraWeb.UserAuth.redirect_if_user_is_authenticated(conn, []) else - redirect(conn, to: ~p"/auth/login") + conn + |> maybe_store_return_to() + |> redirect(to: ~p"/auth/login") end end end diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex index 92b58df4..d8875f21 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -54,8 +54,12 @@ defmodule AlgoraWeb.UserAuth do end defp redirect_require_login(socket) do - socket + + conn = Phoenix.LiveView.get_connect_info(socket).conn + + conn |> LiveView.put_flash(:error, "Please sign in") + |> maybe_store_return_to() |> LiveView.redirect(to: ~p"/auth/login") end @@ -157,13 +161,13 @@ defmodule AlgoraWeb.UserAuth do end end - defp maybe_store_return_to(%{method: "GET"} = conn) do + def maybe_store_return_to(%{method: "GET"} = conn) do %{request_path: request_path, query_string: query_string} = conn return_to = if query_string == "", do: request_path, else: request_path <> "?" <> query_string put_session(conn, :user_return_to, return_to) end - defp maybe_store_return_to(conn), do: conn + def maybe_store_return_to(conn), do: conn def signed_in_path(_conn), do: "/" end diff --git a/lib/algora_web/live/show_live/show.ex b/lib/algora_web/live/show_live/show.ex index 100e8d3a..acd58796 100644 --- a/lib/algora_web/live/show_live/show.ex +++ b/lib/algora_web/live/show_live/show.ex @@ -1,5 +1,6 @@ defmodule AlgoraWeb.ShowLive.Show do use AlgoraWeb, :live_view + import AlgoraWeb.UserAuth, only: [maybe_store_return_to: 1] alias Algora.{Shows, Library, Events} alias Algora.Accounts @@ -344,9 +345,12 @@ defmodule AlgoraWeb.ShowLive.Show do show = Shows.get_show_by_fields!(slug: slug) + conn = Phoenix.LiveView.get_connect_info(socket).conn + cond do current_user == nil -> - socket + conn + |> maybe_store_return_to() |> redirect(to: ~p"/auth/login") current_user.id != show.user_id && !Accounts.admin?(current_user) -> From 93db309b6a40339c361f01ab495f78c16e63e54f Mon Sep 17 00:00:00 2001 From: bryans-go Date: Sat, 28 Sep 2024 18:17:47 +0530 Subject: [PATCH 2/8] fix Signed-off-by: bryans-go --- lib/algora_web/controllers/user_auth.ex | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex index d8875f21..ce475cae 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -54,14 +54,14 @@ defmodule AlgoraWeb.UserAuth do end defp redirect_require_login(socket) do - - conn = Phoenix.LiveView.get_connect_info(socket).conn - conn - |> LiveView.put_flash(:error, "Please sign in") - |> maybe_store_return_to() - |> LiveView.redirect(to: ~p"/auth/login") - end + conn = Phoenix.LiveView.get_connect_info(socket).conn + + conn + |> maybe_store_return_to() # Store the return path before flashing the message + |> LiveView.put_flash(:error, "Please sign in") + |> LiveView.redirect(to: ~p"/auth/login") +end @doc """ Logs the user in. From 2320bdcdfa074db84bdcdd46da6784f207e0c5a1 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Sun, 29 Sep 2024 19:04:16 +0530 Subject: [PATCH 3/8] fix commented issue Signed-off-by: bryans-go --- lib/algora_web/controllers/user_auth.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex index ce475cae..3bd90a42 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -55,10 +55,10 @@ defmodule AlgoraWeb.UserAuth do defp redirect_require_login(socket) do - conn = Phoenix.LiveView.get_connect_info(socket).conn - - conn - |> maybe_store_return_to() # Store the return path before flashing the message + case LiveView.get_connect_info(socket).conn + %{conn: conn} -> + conn + |> maybe_store_return_to() |> LiveView.put_flash(:error, "Please sign in") |> LiveView.redirect(to: ~p"/auth/login") end From 617427f8d35f34b98f98ad7eab70295832e66b39 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Sun, 29 Sep 2024 19:41:58 +0530 Subject: [PATCH 4/8] fix: add fix Signed-off-by: bryans-go --- lib/algora_web/controllers/user_auth.ex | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex index 3bd90a42..e1f91088 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -54,10 +54,13 @@ defmodule AlgoraWeb.UserAuth do end defp redirect_require_login(socket) do + conn = Phoenix.LiveView.get_connect_info(socket)[:conn] + %{request_path: request_path, query_string: query_string} = conn - case LiveView.get_connect_info(socket).conn - %{conn: conn} -> - conn + return_to = if query_string == "", do: request_path, else: request_path <> "?" <> query_string + socket = Phoenix.LiveView.put_session(socket, :user_return_to, return_to) + + socket |> maybe_store_return_to() |> LiveView.put_flash(:error, "Please sign in") |> LiveView.redirect(to: ~p"/auth/login") From 4dce49d3b97ce8369631705865d502696342fa57 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Sun, 29 Sep 2024 19:44:31 +0530 Subject: [PATCH 5/8] fix Signed-off-by: bryans-go --- lib/algora_web/controllers/user_auth.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex index e1f91088..20d247bd 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -56,12 +56,11 @@ defmodule AlgoraWeb.UserAuth do defp redirect_require_login(socket) do conn = Phoenix.LiveView.get_connect_info(socket)[:conn] %{request_path: request_path, query_string: query_string} = conn - + return_to = if query_string == "", do: request_path, else: request_path <> "?" <> query_string socket = Phoenix.LiveView.put_session(socket, :user_return_to, return_to) socket - |> maybe_store_return_to() |> LiveView.put_flash(:error, "Please sign in") |> LiveView.redirect(to: ~p"/auth/login") end From 7f3a739ac6aa9bc6f901c88ba957b2bcc4ac599b Mon Sep 17 00:00:00 2001 From: bryans-go Date: Sun, 29 Sep 2024 20:19:23 +0530 Subject: [PATCH 6/8] fix Signed-off-by: bryans-go --- lib/algora_web/controllers/user_auth.ex | 4 ++-- lib/algora_web/live/show_live/show.ex | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex index 20d247bd..4b52b4c8 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -54,11 +54,11 @@ defmodule AlgoraWeb.UserAuth do end defp redirect_require_login(socket) do - conn = Phoenix.LiveView.get_connect_info(socket)[:conn] + conn = LiveView.get_connect_info(socket, :conn) %{request_path: request_path, query_string: query_string} = conn return_to = if query_string == "", do: request_path, else: request_path <> "?" <> query_string - socket = Phoenix.LiveView.put_session(socket, :user_return_to, return_to) + socket = put_session(socket, :user_return_to, return_to) socket |> LiveView.put_flash(:error, "Please sign in") diff --git a/lib/algora_web/live/show_live/show.ex b/lib/algora_web/live/show_live/show.ex index acd58796..f5382ce9 100644 --- a/lib/algora_web/live/show_live/show.ex +++ b/lib/algora_web/live/show_live/show.ex @@ -345,7 +345,7 @@ defmodule AlgoraWeb.ShowLive.Show do show = Shows.get_show_by_fields!(slug: slug) - conn = Phoenix.LiveView.get_connect_info(socket).conn + conn = get_connect_info(socket, :conn) cond do current_user == nil -> From 8cc0423c1d64b64232d5bab2923071bf500755ef Mon Sep 17 00:00:00 2001 From: bryans-go Date: Sun, 29 Sep 2024 21:28:40 +0530 Subject: [PATCH 7/8] fix Signed-off-by: bryans-go --- lib/algora_web/controllers/user_auth.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex index 4b52b4c8..a475dd2c 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -58,7 +58,7 @@ defmodule AlgoraWeb.UserAuth do %{request_path: request_path, query_string: query_string} = conn return_to = if query_string == "", do: request_path, else: request_path <> "?" <> query_string - socket = put_session(socket, :user_return_to, return_to) + socket = LiveView.put_session(socket, :user_return_to, return_to) socket |> LiveView.put_flash(:error, "Please sign in") From 393e3230bcbe53929ac299f3c247d869168e4a9d Mon Sep 17 00:00:00 2001 From: bryans-go Date: Sun, 29 Sep 2024 21:46:04 +0530 Subject: [PATCH 8/8] fix Signed-off-by: bryans-go --- lib/algora_web/controllers/user_auth.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex index a475dd2c..534aaa86 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -58,11 +58,10 @@ defmodule AlgoraWeb.UserAuth do %{request_path: request_path, query_string: query_string} = conn return_to = if query_string == "", do: request_path, else: request_path <> "?" <> query_string - socket = LiveView.put_session(socket, :user_return_to, return_to) socket |> LiveView.put_flash(:error, "Please sign in") - |> LiveView.redirect(to: ~p"/auth/login") + |> LiveView.redirect(to: ~p"/auth/login?return_to=#{URI.encode_www_form(return_to)}") end @doc """