diff --git a/lib/algora_web/components/layouts/root.html.heex b/lib/algora_web/components/layouts/root.html.heex
index e62628b3..8066497e 100644
--- a/lib/algora_web/components/layouts/root.html.heex
+++ b/lib/algora_web/components/layouts/root.html.heex
@@ -178,7 +178,7 @@
<% else %>
<.link
- navigate="/auth/login"
+ href={~p"/auth/login?#{[return_to: @uri.path]}"}
class="rounded-lg bg-gray-50 hover:bg-gray-200 py-1 px-2 text-sm font-semibold leading-6 text-gray-950 active:text-gray-950/80"
>
Login
diff --git a/lib/algora_web/controllers/redirect_controller.ex b/lib/algora_web/controllers/redirect_controller.ex
index 48d7844e..35b6ff37 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() // stores the return to path in the session
+ |> redirect(to: ~p"/auth/login")
end
end
diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex
index 92b58df4..98ede3eb 100644
--- a/lib/algora_web/controllers/user_auth.ex
+++ b/lib/algora_web/controllers/user_auth.ex
@@ -56,6 +56,7 @@ defmodule AlgoraWeb.UserAuth do
defp redirect_require_login(socket) do
socket
|> LiveView.put_flash(:error, "Please sign in")
+ |> LiveView.put_session(:user_return_to, socket.assigns.uri.path)
|> LiveView.redirect(to: ~p"/auth/login")
end
diff --git a/lib/algora_web/live/sign_in_live.ex b/lib/algora_web/live/sign_in_live.ex
index a8c6e6e0..790efafb 100644
--- a/lib/algora_web/live/sign_in_live.ex
+++ b/lib/algora_web/live/sign_in_live.ex
@@ -9,7 +9,7 @@ defmodule AlgoraWeb.SignInLive do
Algora TV
Sign in with GitHub
@@ -20,6 +20,9 @@ defmodule AlgoraWeb.SignInLive do
end
def mount(_params, _session, socket) do
- {:ok, socket}
+ {:ok,
+ socket
+ |> assign(:page_title, "Sign in")
+ |> assign(:return_to, socket.assigns.uri.query_params["return_to"])}
end
end