From dd825c7fd451932caa6fd9d35a03435d6842d606 Mon Sep 17 00:00:00 2001 From: bryans-go Date: Fri, 27 Sep 2024 22:57:41 +0530 Subject: [PATCH] fix: return to original page after login (#101) Signed-off-by: bryans-go --- lib/algora_web/controllers/redirect_controller.ex | 6 ++++-- lib/algora_web/controllers/user_auth.ex | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) 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..34865e02 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -157,13 +157,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