Fold RequireLogin plug into Auth module
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
defmodule MusicLibraryWeb.Auth do
|
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
|
def correct_login_password?(password) do
|
||||||
Plug.Crypto.secure_compare(correct_login_password(), password)
|
Plug.Crypto.secure_compare(correct_login_password(), password)
|
||||||
end
|
end
|
||||||
@@ -7,4 +11,15 @@ defmodule MusicLibraryWeb.Auth do
|
|||||||
Application.get_env(:music_library, MusicLibraryWeb)
|
Application.get_env(:music_library, MusicLibraryWeb)
|
||||||
|> Keyword.fetch!(:login_password)
|
|> Keyword.fetch!(:login_password)
|
||||||
end
|
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
|
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
|
defmodule MusicLibraryWeb.Router do
|
||||||
use MusicLibraryWeb, :router
|
use MusicLibraryWeb, :router
|
||||||
|
|
||||||
|
import MusicLibraryWeb.Auth, only: [require_logged_in: 2]
|
||||||
|
|
||||||
pipeline :browser do
|
pipeline :browser do
|
||||||
plug :accepts, ["html"]
|
plug :accepts, ["html"]
|
||||||
plug :fetch_session
|
plug :fetch_session
|
||||||
@@ -14,8 +16,8 @@ defmodule MusicLibraryWeb.Router do
|
|||||||
plug :accepts, ["json"]
|
plug :accepts, ["json"]
|
||||||
end
|
end
|
||||||
|
|
||||||
pipeline :require_login do
|
pipeline :logged_in do
|
||||||
plug MusicLibraryWeb.Plug.RequireLogin
|
plug :require_logged_in
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/", MusicLibraryWeb do
|
scope "/", MusicLibraryWeb do
|
||||||
@@ -26,7 +28,7 @@ defmodule MusicLibraryWeb.Router do
|
|||||||
post "/sessions/create", SessionController, :create
|
post "/sessions/create", SessionController, :create
|
||||||
|
|
||||||
scope "/" do
|
scope "/" do
|
||||||
pipe_through :require_login
|
pipe_through :logged_in
|
||||||
|
|
||||||
get "/covers/:record_id", CoverController, :show
|
get "/covers/:record_id", CoverController, :show
|
||||||
|
|
||||||
@@ -56,11 +58,6 @@ defmodule MusicLibraryWeb.Router do
|
|||||||
get "/collection/latest", CollectionController, :latest
|
get "/collection/latest", CollectionController, :latest
|
||||||
end
|
end
|
||||||
|
|
||||||
# Other scopes may use custom stacks.
|
|
||||||
# scope "/api", MusicLibraryWeb do
|
|
||||||
# pipe_through :api
|
|
||||||
# end
|
|
||||||
|
|
||||||
# Enable LiveDashboard in development
|
# Enable LiveDashboard in development
|
||||||
if Application.compile_env(:music_library, :dev_routes) do
|
if Application.compile_env(:music_library, :dev_routes) do
|
||||||
# If you want to use the LiveDashboard in production, you should put
|
# If you want to use the LiveDashboard in production, you should put
|
||||||
@@ -71,7 +68,7 @@ defmodule MusicLibraryWeb.Router do
|
|||||||
import Phoenix.LiveDashboard.Router
|
import Phoenix.LiveDashboard.Router
|
||||||
|
|
||||||
scope "/dev" do
|
scope "/dev" do
|
||||||
pipe_through [:browser, :require_login]
|
pipe_through [:browser, :logged_in]
|
||||||
|
|
||||||
live_dashboard "/dashboard",
|
live_dashboard "/dashboard",
|
||||||
metrics: MusicLibraryWeb.Telemetry,
|
metrics: MusicLibraryWeb.Telemetry,
|
||||||
|
|||||||
@@ -335,7 +335,7 @@ msgstr ""
|
|||||||
msgid "Wishlist"
|
msgid "Wishlist"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: lib/music_library_web/plug/require_login.ex:17
|
#: lib/music_library_web/auth.ex:20
|
||||||
#, elixir-autogen, elixir-format
|
#, elixir-autogen, elixir-format
|
||||||
msgid "You must be logged in to access this page"
|
msgid "You must be logged in to access this page"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|||||||
+14
-20
@@ -1,28 +1,23 @@
|
|||||||
defmodule MusicLibraryWeb.Plug.RequireLoginTest do
|
defmodule MusicLibraryWeb.AuthTest do
|
||||||
use ExUnit.Case, async: true
|
use ExUnit.Case, async: true
|
||||||
use Plug.Test
|
use Plug.Test
|
||||||
|
|
||||||
alias MusicLibraryWeb.Plug.RequireLogin
|
alias MusicLibraryWeb.Auth
|
||||||
|
|
||||||
defp setup_conn(config) do
|
defp setup_conn(_config) do
|
||||||
Map.put(
|
conn =
|
||||||
config,
|
|
||||||
:conn,
|
|
||||||
conn(:get, "/any-path")
|
conn(:get, "/any-path")
|
||||||
|> Phoenix.ConnTest.init_test_session(%{})
|
|> Phoenix.ConnTest.init_test_session(%{})
|
||||||
|> Phoenix.ConnTest.fetch_flash()
|
|> Phoenix.ConnTest.fetch_flash()
|
||||||
)
|
|
||||||
|
%{conn: conn}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp authenticate(%{conn: conn} = config) do
|
describe "require_logged_in/2" do
|
||||||
Map.put(config, :conn, Phoenix.ConnTest.init_test_session(conn, %{logged_in: true}))
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "when logged out" do
|
|
||||||
setup [:setup_conn]
|
setup [:setup_conn]
|
||||||
|
|
||||||
test "it redirects to /login", %{conn: conn} do
|
test "when logged out, it redirects to /login", %{conn: conn} do
|
||||||
conn = RequireLogin.call(conn, [])
|
conn = Auth.require_logged_in(conn, [])
|
||||||
|
|
||||||
{"location", location} =
|
{"location", location} =
|
||||||
conn.resp_headers
|
conn.resp_headers
|
||||||
@@ -34,13 +29,12 @@ defmodule MusicLibraryWeb.Plug.RequireLoginTest do
|
|||||||
assert location == "/login"
|
assert location == "/login"
|
||||||
assert conn.assigns.flash == %{"error" => "You must be logged in to access this page"}
|
assert conn.assigns.flash == %{"error" => "You must be logged in to access this page"}
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe "when logged in" do
|
test "when logged in, it passes through", %{conn: conn} do
|
||||||
setup [:setup_conn, :authenticate]
|
conn =
|
||||||
|
conn
|
||||||
test "it passes through", %{conn: conn} do
|
|> Phoenix.ConnTest.init_test_session(%{logged_in: true})
|
||||||
conn = RequireLogin.call(conn, [])
|
|> Auth.require_logged_in([])
|
||||||
|
|
||||||
assert conn.status == nil
|
assert conn.status == nil
|
||||||
assert conn.state == :unset
|
assert conn.state == :unset
|
||||||
Reference in New Issue
Block a user