Skip to content

Commit

Permalink
fix: error flash message is always displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
midigofrank authored and stuartc committed Nov 15, 2023
1 parent 4475c1a commit 4e8748f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/lightning_web/controllers/user_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ defmodule LightningWeb.UserAuth do
conn
|> renew_session()
|> delete_resp_cookie(@remember_me_cookie)
|> redirect(to: "/")
|> redirect(to: "/users/log_in")
end

@doc """
Expand Down Expand Up @@ -223,7 +223,7 @@ defmodule LightningWeb.UserAuth do

_ ->
conn
|> put_flash(:error, "You must log in to access this page.")
# |> put_flash(:error, "You must log in to access this page.")
|> maybe_store_return_to()
|> redirect(to: Routes.user_session_path(conn, :new))
|> halt()
Expand Down
8 changes: 4 additions & 4 deletions test/lightning_web/controllers/user_auth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ defmodule LightningWeb.UserAuthTest do
refute get_session(conn, :user_token)
refute conn.cookies[@remember_me_cookie]
assert %{max_age: 0} = conn.resp_cookies[@remember_me_cookie]
assert redirected_to(conn) == "/"
assert redirected_to(conn) == "/users/log_in"
refute Accounts.get_user_by_session_token(user_token)
end

Expand All @@ -128,7 +128,7 @@ defmodule LightningWeb.UserAuthTest do
conn = conn |> fetch_cookies() |> UserAuth.log_out_user()
refute get_session(conn, :user_token)
assert %{max_age: 0} = conn.resp_cookies[@remember_me_cookie]
assert redirected_to(conn) == "/"
assert redirected_to(conn) == "/users/log_in"
end
end

Expand Down Expand Up @@ -197,8 +197,8 @@ defmodule LightningWeb.UserAuthTest do
assert conn.halted
assert redirected_to(conn) == Routes.user_session_path(conn, :new)

assert Phoenix.Flash.get(conn.assigns.flash, :error) ==
"You must log in to access this page."
# flash message disabled
assert Phoenix.Flash.get(conn.assigns.flash, :error) |> is_nil()
end

test "returns a 401 on json requests if user is not authenticated", %{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ defmodule LightningWeb.UserSessionControllerTest do
conn =
conn |> log_in_user(user) |> get(Routes.user_session_path(conn, :delete))

assert redirected_to(conn) == "/"
assert redirected_to(conn) == "/users/log_in"
refute get_session(conn, :user_token)

assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
Expand All @@ -262,7 +262,7 @@ defmodule LightningWeb.UserSessionControllerTest do

test "succeeds even if the user is not logged in", %{conn: conn} do
conn = get(conn, Routes.user_session_path(conn, :delete))
assert redirected_to(conn) == "/"
assert redirected_to(conn) == "/users/log_in"
refute get_session(conn, :user_token)

assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
Expand Down

0 comments on commit 4e8748f

Please sign in to comment.