From 66dee79e071437f9855d98b8a636420b1749bede Mon Sep 17 00:00:00 2001 From: Max Veytsman Date: Mon, 17 Jun 2024 17:40:19 -0400 Subject: [PATCH] Test and refactor for cancel --- .../controllers/authentication_controller.ex | 14 +++++++------- .../controllers/authentication_html/show.html.heex | 3 ++- lib/bike_brigade_web/core_components.ex | 2 +- lib/bike_brigade_web/router.ex | 1 + .../controllers/authentication_controller_test.exs | 7 +++++++ 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/bike_brigade_web/controllers/authentication_controller.ex b/lib/bike_brigade_web/controllers/authentication_controller.ex index bdc52d8a..2d2980b8 100644 --- a/lib/bike_brigade_web/controllers/authentication_controller.ex +++ b/lib/bike_brigade_web/controllers/authentication_controller.ex @@ -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{}) @@ -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 diff --git a/lib/bike_brigade_web/controllers/authentication_html/show.html.heex b/lib/bike_brigade_web/controllers/authentication_html/show.html.heex index 7caf9264..ab0dde06 100644 --- a/lib/bike_brigade_web/controllers/authentication_html/show.html.heex +++ b/lib/bike_brigade_web/controllers/authentication_html/show.html.heex @@ -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 diff --git a/lib/bike_brigade_web/core_components.ex b/lib/bike_brigade_web/core_components.ex index 3511b263..10d10793 100644 --- a/lib/bike_brigade_web/core_components.ex +++ b/lib/bike_brigade_web/core_components.ex @@ -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 [ diff --git a/lib/bike_brigade_web/router.ex b/lib/bike_brigade_web/router.ex index 617151a9..00f7db15 100644 --- a/lib/bike_brigade_web/router.ex +++ b/lib/bike_brigade_web/router.ex @@ -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). diff --git a/test/bike_brigade_web/controllers/authentication_controller_test.exs b/test/bike_brigade_web/controllers/authentication_controller_test.exs index 2d7301d7..eba8c9a3 100644 --- a/test/bike_brigade_web/controllers/authentication_controller_test.exs +++ b/test/bike_brigade_web/controllers/authentication_controller_test.exs @@ -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