Skip to content

Commit

Permalink
Redirect to previous page after logging in (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekingfrog authored Jun 9, 2024
1 parent 902fbad commit 1d4c102
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/teiserver/account/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
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 1d4c102

Please sign in to comment.