Skip to content

Commit

Permalink
Redirect to previous page after logging in (beyond-all-reason#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekingfrog authored and StanczakDominik committed Jun 9, 2024
1 parent 902fbad commit 47b2ba7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/teiserver/account/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ 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)
)
Expand Down
7 changes: 1 addition & 6 deletions lib/teiserver_web/components/nav_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,7 @@ defmodule TeiserverWeb.NavComponents do
~H"""
<div class="col sub-menu-icon">
<a href={@url} class={"block-link #{@active_class}"}>
<Fontawesome.icon
:if={@icon}
icon={@icon}
style="solid"
size="2x"
/><br />
<Fontawesome.icon :if={@icon} icon={@icon} style="solid" size="2x" /><br />
<%= render_slot(@inner_block) %>
</a>
</div>
Expand Down
5 changes: 4 additions & 1 deletion lib/teiserver_web/controllers/account/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 47b2ba7

Please sign in to comment.