From 1d4c10276764fec726e4ee152fd487dc5467acaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Sun, 9 Jun 2024 11:03:14 +0100 Subject: [PATCH] Redirect to previous page after logging in (#309) --- lib/teiserver/account/error_handler.ex | 2 ++ lib/teiserver_web/controllers/account/session_controller.ex | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/teiserver/account/error_handler.ex b/lib/teiserver/account/error_handler.ex index 775327a03..4d9608b7b 100644 --- a/lib/teiserver/account/error_handler.ex +++ b/lib/teiserver/account/error_handler.ex @@ -6,7 +6,9 @@ defmodule Teiserver.Account.ErrorHandler do @impl Guardian.Plug.ErrorHandler def auth_error(conn, {:unauthenticated, _reason}, _opts) do + redirect_to = "#{conn.request_path}?#{conn.query_string}" conn + |> put_resp_cookie("_redirect_to", redirect_to, sign: true, max_age: 60*5) |> Phoenix.Controller.redirect( to: TeiserverWeb.Router.Helpers.account_session_path(conn, :login) ) diff --git a/lib/teiserver_web/controllers/account/session_controller.ex b/lib/teiserver_web/controllers/account/session_controller.ex index 158b9654e..7112d4d41 100644 --- a/lib/teiserver_web/controllers/account/session_controller.ex +++ b/lib/teiserver_web/controllers/account/session_controller.ex @@ -100,11 +100,14 @@ defmodule TeiserverWeb.Account.SessionController do end defp login_reply({:ok, user}, conn) do + cookies = Plug.Conn.fetch_cookies(conn, signed: ~w(_redirect_to)).cookies + conn |> put_flash(:info, "Welcome back!") |> Guardian.Plug.sign_in(user) |> Guardian.Plug.remember_me(user) - |> redirect(to: "/") + |> Plug.Conn.delete_resp_cookie("_redirect_to", sign: true) + |> redirect(to: Map.get(cookies, "_redirect_to", "/")) end defp login_reply({:error, reason}, conn) do