Complete authentication flow with dummy password
This commit is contained in:
@@ -11,7 +11,9 @@ defmodule MusicLibraryWeb.SessionController do
|
|||||||
password = params["password"]
|
password = params["password"]
|
||||||
|
|
||||||
if password == "password" do
|
if password == "password" do
|
||||||
redirect(conn, to: "/")
|
conn
|
||||||
|
|> put_session(:logged_in, true)
|
||||||
|
|> redirect(to: "/")
|
||||||
else
|
else
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Invalid password")
|
|> put_flash(:error, "Invalid password")
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -14,16 +14,23 @@ defmodule MusicLibraryWeb.Router do
|
|||||||
plug :accepts, ["json"]
|
plug :accepts, ["json"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
pipeline :require_login do
|
||||||
|
plug MusicLibraryWeb.Plug.RequireLogin
|
||||||
|
end
|
||||||
|
|
||||||
scope "/", MusicLibraryWeb do
|
scope "/", MusicLibraryWeb do
|
||||||
pipe_through :browser
|
pipe_through :browser
|
||||||
|
|
||||||
get "/health", HealthController, :index
|
get "/health", HealthController, :index
|
||||||
get "/covers/:record_id", CoverController, :show
|
|
||||||
get "/", StatsController, :index
|
|
||||||
|
|
||||||
get "/login", SessionController, :new
|
get "/login", SessionController, :new
|
||||||
post "/sessions/create", SessionController, :create
|
post "/sessions/create", SessionController, :create
|
||||||
|
|
||||||
|
scope "/" do
|
||||||
|
pipe_through :require_login
|
||||||
|
|
||||||
|
get "/covers/:record_id", CoverController, :show
|
||||||
|
get "/", StatsController, :index
|
||||||
|
|
||||||
live "/records", RecordLive.Index, :index
|
live "/records", RecordLive.Index, :index
|
||||||
live "/records/import", RecordLive.Index, :import
|
live "/records/import", RecordLive.Index, :import
|
||||||
live "/records/:id/edit", RecordLive.Index, :edit
|
live "/records/:id/edit", RecordLive.Index, :edit
|
||||||
@@ -31,6 +38,7 @@ defmodule MusicLibraryWeb.Router do
|
|||||||
live "/records/:id", RecordLive.Show, :show
|
live "/records/:id", RecordLive.Show, :show
|
||||||
live "/records/:id/show/edit", RecordLive.Show, :edit
|
live "/records/:id/show/edit", RecordLive.Show, :edit
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Other scopes may use custom stacks.
|
# Other scopes may use custom stacks.
|
||||||
# scope "/api", MusicLibraryWeb do
|
# scope "/api", MusicLibraryWeb do
|
||||||
|
|||||||
@@ -33,6 +33,11 @@ defmodule MusicLibraryWeb.ConnCase do
|
|||||||
|
|
||||||
setup tags do
|
setup tags do
|
||||||
MusicLibrary.DataCase.setup_sandbox(tags)
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user