Complete authentication flow with dummy password

This commit is contained in:
Claudio Ortolina
2024-10-20 19:25:31 +01:00
parent b5bceb92e0
commit 4fcfc6c921
4 changed files with 46 additions and 10 deletions
@@ -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")
@@ -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
+16 -8
View File
@@ -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.