Skip to content

Commit

Permalink
Add basic_auth to admin routes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkonidas committed Jun 19, 2024
1 parent 63672e7 commit 1dfe242
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ config :plexus,

config :plexus, :generators, api_prefix: "/api/v1"

config :plexus, :basic_auth,
username: "plexus",
password: "plexus"

config :plexus, Plexus.Repo,
migration_primary_key: [id: :uuid, type: :binary_id],
migration_timestamps: [type: :utc_datetime_usec]
Expand Down
4 changes: 4 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ if System.get_env("PHX_SERVER") do
end

if config_env() == :prod do
config :plexus, :basic_auth,
username: System.fetch_env!("BASIC_AUTH_USERNAME"),
password: System.fetch_env!("BASIC_AUTH_PASSWORD")

database_url =
System.get_env("DATABASE_URL") ||
raise """
Expand Down
11 changes: 10 additions & 1 deletion lib/plexus_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ defmodule PlexusWeb.Router do
plug PlexusWeb.APIAuthPlug
end

pipeline :ensure_auth do
plug :auth
end

scope "/", PlexusWeb do
pipe_through :browser

get "/", PageController, :home
end

scope "/admin", PlexusWeb.Admin do
pipe_through :browser
pipe_through [:browser, :ensure_auth]

live "/apps", AppLive.Index, :index
live "/apps/new", AppLive.Index, :new
Expand Down Expand Up @@ -74,4 +78,9 @@ defmodule PlexusWeb.Router do
forward "/mailbox", Plug.Swoosh.MailboxPreview
end
end

defp auth(conn, _opts) do
[username: username, password: password] = Application.fetch_env!(:plexus, :basic_auth)
Plug.BasicAuth.basic_auth(conn, username: username, password: password)
end
end

0 comments on commit 1dfe242

Please sign in to comment.