diff --git a/lib/microwaveprop/accounts/user.ex b/lib/microwaveprop/accounts/user.ex index e8fa9573..36e1fabe 100644 --- a/lib/microwaveprop/accounts/user.ex +++ b/lib/microwaveprop/accounts/user.ex @@ -131,7 +131,7 @@ defmodule Microwaveprop.Accounts.User do defp validate_password(changeset, opts) do changeset |> validate_required([:password]) - |> validate_length(:password, min: 12, max: 72) + |> validate_length(:password, min: 8, max: 72) # Examples of additional password validation: # |> validate_format(:password, ~r/[a-z]/, message: "at least one lower case character") # |> validate_format(:password, ~r/[A-Z]/, message: "at least one upper case character") diff --git a/lib/microwaveprop_web.ex b/lib/microwaveprop_web.ex index dcbeadda..34fa33e6 100644 --- a/lib/microwaveprop_web.ex +++ b/lib/microwaveprop_web.ex @@ -52,6 +52,8 @@ defmodule MicrowavepropWeb do quote do use Phoenix.LiveView + on_mount MicrowavepropWeb.UserAuth + unquote(html_helpers()) end end diff --git a/lib/microwaveprop_web/components/layouts.ex b/lib/microwaveprop_web/components/layouts.ex index 1459daad..0c55d563 100644 --- a/lib/microwaveprop_web/components/layouts.ex +++ b/lib/microwaveprop_web/components/layouts.ex @@ -46,6 +46,18 @@ defmodule MicrowavepropWeb.Layouts do <.link navigate="/contacts/map" class="btn btn-ghost btn-sm">Contact Map <.link navigate="/contacts" class="btn btn-ghost btn-sm">Contacts <.link navigate="/submit" class="btn btn-ghost btn-sm">Submit + <%= if @current_scope && @current_scope.user do %> + + {@current_scope.user.callsign} + + <.link href={~p"/users/settings"} class="btn btn-ghost btn-sm">Settings + <.link href={~p"/users/log-out"} method="delete" class="btn btn-ghost btn-sm"> + Log out + + <% else %> + <.link href={~p"/users/register"} class="btn btn-ghost btn-sm">Register + <.link href={~p"/users/log-in"} class="btn btn-ghost btn-sm">Log in + <% end %>
diff --git a/lib/microwaveprop_web/components/layouts/root.html.heex b/lib/microwaveprop_web/components/layouts/root.html.heex index df5b44c4..9e582bdb 100644 --- a/lib/microwaveprop_web/components/layouts/root.html.heex +++ b/lib/microwaveprop_web/components/layouts/root.html.heex @@ -37,26 +37,6 @@ - {@inner_content} diff --git a/lib/microwaveprop_web/live/algo_live.ex b/lib/microwaveprop_web/live/algo_live.ex index b9bbaf45..a883f513 100644 --- a/lib/microwaveprop_web/live/algo_live.ex +++ b/lib/microwaveprop_web/live/algo_live.ex @@ -13,7 +13,7 @@ defmodule MicrowavepropWeb.AlgoLive do @impl true def render(assigns) do ~H""" - +
{raw(@content)}
diff --git a/lib/microwaveprop_web/live/backfill_live.ex b/lib/microwaveprop_web/live/backfill_live.ex index c20e3f48..738f7aac 100644 --- a/lib/microwaveprop_web/live/backfill_live.ex +++ b/lib/microwaveprop_web/live/backfill_live.ex @@ -311,7 +311,7 @@ defmodule MicrowavepropWeb.BackfillLive do @impl true def render(assigns) do ~H""" - + <.header> Backfill Dashboard <:subtitle>Enqueue and monitor contact enrichment jobs diff --git a/lib/microwaveprop_web/live/contact_live/index.ex b/lib/microwaveprop_web/live/contact_live/index.ex index 70975558..f877cb11 100644 --- a/lib/microwaveprop_web/live/contact_live/index.ex +++ b/lib/microwaveprop_web/live/contact_live/index.ex @@ -112,7 +112,7 @@ defmodule MicrowavepropWeb.ContactLive.Index do @impl true def render(assigns) do ~H""" - + <.header> Contacts <:subtitle>{@total_entries} contacts diff --git a/lib/microwaveprop_web/live/contact_live/show.ex b/lib/microwaveprop_web/live/contact_live/show.ex index 3a41b8c3..dc7f07ca 100644 --- a/lib/microwaveprop_web/live/contact_live/show.ex +++ b/lib/microwaveprop_web/live/contact_live/show.ex @@ -435,7 +435,7 @@ defmodule MicrowavepropWeb.ContactLive.Show do @impl true def render(assigns) do ~H""" - + <.header> {@contact.station1} / {@contact.station2} diff --git a/lib/microwaveprop_web/live/path_live.ex b/lib/microwaveprop_web/live/path_live.ex index 99234509..fe20b1eb 100644 --- a/lib/microwaveprop_web/live/path_live.ex +++ b/lib/microwaveprop_web/live/path_live.ex @@ -466,7 +466,7 @@ defmodule MicrowavepropWeb.PathLive do @impl true def render(assigns) do ~H""" - + <.header> Path Calculator <:subtitle>Analyze microwave propagation between two points diff --git a/lib/microwaveprop_web/live/submit_live.ex b/lib/microwaveprop_web/live/submit_live.ex index 3cd31ae3..e27773a1 100644 --- a/lib/microwaveprop_web/live/submit_live.ex +++ b/lib/microwaveprop_web/live/submit_live.ex @@ -127,7 +127,7 @@ defmodule MicrowavepropWeb.SubmitLive do @impl true def render(assigns) do ~H""" - + <.header> Submit Contact <:subtitle>Help us build a better propagation model diff --git a/lib/microwaveprop_web/user_auth.ex b/lib/microwaveprop_web/user_auth.ex index 0e37875e..442ac611 100644 --- a/lib/microwaveprop_web/user_auth.ex +++ b/lib/microwaveprop_web/user_auth.ex @@ -18,6 +18,28 @@ defmodule MicrowavepropWeb.UserAuth do same_site: "Lax" ] + @doc """ + LiveView `on_mount` callback that assigns `:current_scope` from the + session token so LiveView templates can show auth-aware navigation. + """ + def on_mount(:default, _params, session, socket) do + {:cont, mount_current_scope(socket, session)} + end + + defp mount_current_scope(socket, session) do + Phoenix.Component.assign_new(socket, :current_scope, fn -> + user = + with token when is_binary(token) <- session["user_token"], + {user, _inserted_at} <- Accounts.get_user_by_session_token(token) do + user + else + _ -> nil + end + + Scope.for_user(user) + end) + end + # How old the session token should be before a new one is issued. When a request is made # with a session token older than this value, then a new session token will be created # and the session and remember-me cookies (if set) will be updated with the new token. diff --git a/test/microwaveprop/accounts_test.exs b/test/microwaveprop/accounts_test.exs index 8877cf4f..530f956c 100644 --- a/test/microwaveprop/accounts_test.exs +++ b/test/microwaveprop/accounts_test.exs @@ -120,7 +120,7 @@ defmodule Microwaveprop.AccountsTest do {:error, changeset} = Accounts.register_user(valid_user_attributes(password: "short")) - assert "should be at least 12 character(s)" in errors_on(changeset).password + assert "should be at least 8 character(s)" in errors_on(changeset).password end test "registers a valid user but leaves them unconfirmed" do @@ -323,12 +323,12 @@ defmodule Microwaveprop.AccountsTest do test "validates password", %{user: user} do {:error, changeset} = Accounts.update_user_password(user, %{ - password: "not valid", - password_confirmation: "another" + password: "short", + password_confirmation: "other" }) assert %{ - password: ["should be at least 12 character(s)"], + password: ["should be at least 8 character(s)"], password_confirmation: ["does not match password"] } = errors_on(changeset) end diff --git a/test/microwaveprop_web/controllers/user_settings_controller_test.exs b/test/microwaveprop_web/controllers/user_settings_controller_test.exs index 6331722e..fea1ad4e 100644 --- a/test/microwaveprop_web/controllers/user_settings_controller_test.exs +++ b/test/microwaveprop_web/controllers/user_settings_controller_test.exs @@ -56,14 +56,14 @@ defmodule MicrowavepropWeb.UserSettingsControllerTest do put(conn, ~p"/users/settings", %{ "action" => "update_password", "user" => %{ - "password" => "too short", - "password_confirmation" => "does not match" + "password" => "short", + "password_confirmation" => "nope" } }) response = html_response(old_password_conn, 200) assert response =~ "Settings" - assert response =~ "should be at least 12 character(s)" + assert response =~ "should be at least 8 character(s)" assert response =~ "does not match password" assert get_session(old_password_conn, :user_token) == get_session(conn, :user_token)