Skip to content

Commit

Permalink
Test and refactor for cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
mveytsman committed Jun 17, 2024
1 parent 3644284 commit 66dee79
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/bike_brigade_web/controllers/authentication_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ defmodule BikeBrigadeWeb.AuthenticationController do
end
end

def show(conn, %{"cancel" => "true", "phone" => phone}) do
AuthenticationMessenger.clear_token(phone)

conn
|> redirect(to: ~p"/login")
end

def show(conn, _params) do
changeset = Ecto.Changeset.change(%Login{})

Expand Down Expand Up @@ -106,6 +99,13 @@ defmodule BikeBrigadeWeb.AuthenticationController do
end
end

def cancel(conn, %{"phone" => phone}) do
AuthenticationMessenger.clear_token(phone)

conn
|> redirect(to: ~p"/login")
end

@doc "Set the session token and live socket for the user"
def do_login(conn, user) do
conn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
<.button
color={:white}
class="w-full"
href={~p"/login?cancel=true&phone=#{@changeset.data.phone}"}
href={~p"/login?phone=#{@changeset.data.phone}"}
method="delete"
>
Cancel
</.button>
Expand Down
2 changes: 1 addition & 1 deletion lib/bike_brigade_web/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule BikeBrigadeWeb.CoreComponents do
values: [:none, :small, :normal, :medium, :full]

attr :class, :string, default: nil
attr :rest, :global, include: ~w(href patch navigate disabled replace)
attr :rest, :global, include: ~w(href patch navigate disabled replace method)
slot :inner_block, required: true

@button_base_classes [
Expand Down
1 change: 1 addition & 0 deletions lib/bike_brigade_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ defmodule BikeBrigadeWeb.Router do

get "/login", AuthenticationController, :show
post "/login", AuthenticationController, :login
delete "/login", AuthenticationController, :cancel
end

# this scope is for authenticated users that aren't dispatchers (ie: riders).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@ defmodule BikeBrigadeWeb.AuthenticationControllerTest do
conn = get(conn, ~p"/home")
assert html_response(conn, 200) =~ "Welcome!"
end

test "lets you cancel a login", %{conn: conn, user: user} do
conn = post(conn, ~p"/login", %{login: %{phone: user.phone}})
conn = delete(conn, ~p"/login", %{phone: user.phone})
assert redirected_to(conn) == ~p"/login"
assert Map.get(BikeBrigade.AuthenticationMessenger.get_state(), user.phone) == nil
end
end
end

0 comments on commit 66dee79

Please sign in to comment.