From 4fcfc6c9212a534c626fb723b553e280f5cf74e2 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sun, 20 Oct 2024 19:25:31 +0100 Subject: [PATCH] Complete authentication flow with dummy password --- .../controllers/session_controller.ex | 4 +++- lib/music_library_web/plug/require_login.ex | 21 ++++++++++++++++ lib/music_library_web/router.ex | 24 ++++++++++++------- test/support/conn_case.ex | 7 +++++- 4 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 lib/music_library_web/plug/require_login.ex diff --git a/lib/music_library_web/controllers/session_controller.ex b/lib/music_library_web/controllers/session_controller.ex index a340d55c..2e5f9ac2 100644 --- a/lib/music_library_web/controllers/session_controller.ex +++ b/lib/music_library_web/controllers/session_controller.ex @@ -11,7 +11,9 @@ defmodule MusicLibraryWeb.SessionController do password = params["password"] if password == "password" do - redirect(conn, to: "/") + conn + |> put_session(:logged_in, true) + |> redirect(to: "/") else conn |> put_flash(:error, "Invalid password") diff --git a/lib/music_library_web/plug/require_login.ex b/lib/music_library_web/plug/require_login.ex new file mode 100644 index 00000000..5bcd292f --- /dev/null +++ b/lib/music_library_web/plug/require_login.ex @@ -0,0 +1,21 @@ +defmodule MusicLibraryWeb.Plug.RequireLogin do + @behaviour Plug + + import Plug.Conn + import Phoenix.Controller, only: [put_flash: 3, redirect: 2] + + @impl true + def init(opts), do: opts + + @impl true + def call(conn, _opts) do + if get_session(conn, :logged_in) do + conn + else + conn + |> put_flash(:error, "You must be logged in to access this page") + |> redirect(to: "/login") + |> halt() + end + end +end diff --git a/lib/music_library_web/router.ex b/lib/music_library_web/router.ex index e5817f48..224d123c 100644 --- a/lib/music_library_web/router.ex +++ b/lib/music_library_web/router.ex @@ -14,22 +14,30 @@ defmodule MusicLibraryWeb.Router do plug :accepts, ["json"] end + pipeline :require_login do + plug MusicLibraryWeb.Plug.RequireLogin + end + scope "/", MusicLibraryWeb do pipe_through :browser get "/health", HealthController, :index - get "/covers/:record_id", CoverController, :show - get "/", StatsController, :index - get "/login", SessionController, :new post "/sessions/create", SessionController, :create - live "/records", RecordLive.Index, :index - live "/records/import", RecordLive.Index, :import - live "/records/:id/edit", RecordLive.Index, :edit + scope "/" do + pipe_through :require_login - live "/records/:id", RecordLive.Show, :show - live "/records/:id/show/edit", RecordLive.Show, :edit + get "/covers/:record_id", CoverController, :show + get "/", StatsController, :index + + live "/records", RecordLive.Index, :index + live "/records/import", RecordLive.Index, :import + live "/records/:id/edit", RecordLive.Index, :edit + + live "/records/:id", RecordLive.Show, :show + live "/records/:id/show/edit", RecordLive.Show, :edit + end end # Other scopes may use custom stacks. diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 35cac466..0ffbd526 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -33,6 +33,11 @@ defmodule MusicLibraryWeb.ConnCase do setup tags do MusicLibrary.DataCase.setup_sandbox(tags) - {:ok, conn: Phoenix.ConnTest.build_conn()} + + conn = + Phoenix.ConnTest.build_conn() + |> Phoenix.ConnTest.init_test_session(%{logged_in: true}) + + {:ok, conn: conn} end end