Fold RequireLogin plug into Auth module
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
defmodule MusicLibraryWeb.Auth do
|
||||
use Gettext, backend: MusicLibraryWeb.Gettext
|
||||
import Plug.Conn
|
||||
import Phoenix.Controller, only: [put_flash: 3, redirect: 2]
|
||||
|
||||
def correct_login_password?(password) do
|
||||
Plug.Crypto.secure_compare(correct_login_password(), password)
|
||||
end
|
||||
@@ -7,4 +11,15 @@ defmodule MusicLibraryWeb.Auth do
|
||||
Application.get_env(:music_library, MusicLibraryWeb)
|
||||
|> Keyword.fetch!(:login_password)
|
||||
end
|
||||
|
||||
def require_logged_in(conn, _opts) do
|
||||
if get_session(conn, :logged_in) do
|
||||
conn
|
||||
else
|
||||
conn
|
||||
|> put_flash(:error, gettext("You must be logged in to access this page"))
|
||||
|> redirect(to: "/login")
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
defmodule MusicLibraryWeb.Plug.RequireLogin do
|
||||
@behaviour Plug
|
||||
|
||||
use Gettext, backend: MusicLibraryWeb.Gettext
|
||||
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, gettext("You must be logged in to access this page"))
|
||||
|> redirect(to: "/login")
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule MusicLibraryWeb.Router do
|
||||
use MusicLibraryWeb, :router
|
||||
|
||||
import MusicLibraryWeb.Auth, only: [require_logged_in: 2]
|
||||
|
||||
pipeline :browser do
|
||||
plug :accepts, ["html"]
|
||||
plug :fetch_session
|
||||
@@ -14,8 +16,8 @@ defmodule MusicLibraryWeb.Router do
|
||||
plug :accepts, ["json"]
|
||||
end
|
||||
|
||||
pipeline :require_login do
|
||||
plug MusicLibraryWeb.Plug.RequireLogin
|
||||
pipeline :logged_in do
|
||||
plug :require_logged_in
|
||||
end
|
||||
|
||||
scope "/", MusicLibraryWeb do
|
||||
@@ -26,7 +28,7 @@ defmodule MusicLibraryWeb.Router do
|
||||
post "/sessions/create", SessionController, :create
|
||||
|
||||
scope "/" do
|
||||
pipe_through :require_login
|
||||
pipe_through :logged_in
|
||||
|
||||
get "/covers/:record_id", CoverController, :show
|
||||
|
||||
@@ -56,11 +58,6 @@ defmodule MusicLibraryWeb.Router do
|
||||
get "/collection/latest", CollectionController, :latest
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
# scope "/api", MusicLibraryWeb do
|
||||
# pipe_through :api
|
||||
# end
|
||||
|
||||
# Enable LiveDashboard in development
|
||||
if Application.compile_env(:music_library, :dev_routes) do
|
||||
# If you want to use the LiveDashboard in production, you should put
|
||||
@@ -71,7 +68,7 @@ defmodule MusicLibraryWeb.Router do
|
||||
import Phoenix.LiveDashboard.Router
|
||||
|
||||
scope "/dev" do
|
||||
pipe_through [:browser, :require_login]
|
||||
pipe_through [:browser, :logged_in]
|
||||
|
||||
live_dashboard "/dashboard",
|
||||
metrics: MusicLibraryWeb.Telemetry,
|
||||
|
||||
Reference in New Issue
Block a user