diff --git a/lib/mv_web/components/layouts.ex b/lib/mv_web/components/layouts.ex index 1495bb1..4baae08 100644 --- a/lib/mv_web/components/layouts.ex +++ b/lib/mv_web/components/layouts.ex @@ -14,7 +14,8 @@ defmodule MvWeb.Layouts do embed_templates "layouts/*" @doc """ - Renders the app layout + Renders the app layout. Can be used with or without a current_user. + When current_user is present, it will show the navigation bar. ## Examples @@ -22,18 +23,25 @@ defmodule MvWeb.Layouts do

Content

+ +

Authenticated Content

+ + """ attr :flash, :map, required: true, doc: "the map of flash messages" + attr :current_user, :map, default: nil, doc: "the current user, if authenticated" attr :current_scope, :map, - default: nil, - doc: "the current [scope](https://hexdocs.pm/phoenix/scopes.html)" + default: nil, + doc: "the current [scope](https://hexdocs.pm/phoenix/scopes.html)" slot :inner_block, required: true def app(assigns) do ~H""" - <.navbar /> + <%= if @current_user do %> + <.navbar current_user={@current_user} /> + <% end %>
{render_slot(@inner_block)} @@ -44,6 +52,7 @@ defmodule MvWeb.Layouts do """ end + @doc """ Shows the flash group with standard titles and content. diff --git a/lib/mv_web/components/layouts/navbar.ex b/lib/mv_web/components/layouts/navbar.ex index b917ddc..47b4f2e 100644 --- a/lib/mv_web/components/layouts/navbar.ex +++ b/lib/mv_web/components/layouts/navbar.ex @@ -4,6 +4,9 @@ defmodule MvWeb.Layouts.Navbar do """ use Phoenix.Component use Gettext, backend: MvWeb.Gettext + use MvWeb, :verified_routes + + attr :current_user, :map, required: true, doc: "The current user - navbar is only shown when user is present" def navbar(assigns) do ~H""" @@ -65,12 +68,14 @@ defmodule MvWeb.Layouts.Navbar do class="menu menu-sm dropdown-content bg-base-100 rounded-box z-1 mt-3 w-52 p-2 shadow" >
  • - + <.link navigate={~p"/users/#{@current_user.id}"}> {gettext("Profil")} - +
  • {gettext("Settings")}
  • -
  • {gettext("Logout")}
  • +
  • + <.link href={~p"/sign-out"}>{gettext("Logout")} +
  • diff --git a/lib/mv_web/live/member_live/form.ex b/lib/mv_web/live/member_live/form.ex index a526fc3..521d501 100644 --- a/lib/mv_web/live/member_live/form.ex +++ b/lib/mv_web/live/member_live/form.ex @@ -4,7 +4,7 @@ defmodule MvWeb.MemberLive.Form do @impl true def render(assigns) do ~H""" - + <.header> {@page_title} <:subtitle> diff --git a/lib/mv_web/live/member_live/index.html.heex b/lib/mv_web/live/member_live/index.html.heex index fc38889..ab117ee 100644 --- a/lib/mv_web/live/member_live/index.html.heex +++ b/lib/mv_web/live/member_live/index.html.heex @@ -1,4 +1,4 @@ - + <.header> {gettext("Members")} <:actions> diff --git a/lib/mv_web/live/member_live/show.ex b/lib/mv_web/live/member_live/show.ex index 304709c..fbf5b4a 100644 --- a/lib/mv_web/live/member_live/show.ex +++ b/lib/mv_web/live/member_live/show.ex @@ -5,7 +5,7 @@ defmodule MvWeb.MemberLive.Show do @impl true def render(assigns) do ~H""" - + <.header> {@member.first_name} {@member.last_name} <:subtitle>{gettext("This is a member record from your database.")} diff --git a/lib/mv_web/live/property_live/form.ex b/lib/mv_web/live/property_live/form.ex index 42814a3..a60a2e4 100644 --- a/lib/mv_web/live/property_live/form.ex +++ b/lib/mv_web/live/property_live/form.ex @@ -4,7 +4,7 @@ defmodule MvWeb.PropertyLive.Form do @impl true def render(assigns) do ~H""" - + <.header> {@page_title} <:subtitle>{gettext("Use this form to manage property records in your database.")} diff --git a/lib/mv_web/live/property_live/index.ex b/lib/mv_web/live/property_live/index.ex index 7e27344..70171ef 100644 --- a/lib/mv_web/live/property_live/index.ex +++ b/lib/mv_web/live/property_live/index.ex @@ -4,7 +4,7 @@ defmodule MvWeb.PropertyLive.Index do @impl true def render(assigns) do ~H""" - + <.header> Listing Properties <:actions> diff --git a/lib/mv_web/live/property_live/show.ex b/lib/mv_web/live/property_live/show.ex index 6d9bb1a..2a1e2ec 100644 --- a/lib/mv_web/live/property_live/show.ex +++ b/lib/mv_web/live/property_live/show.ex @@ -4,7 +4,7 @@ defmodule MvWeb.PropertyLive.Show do @impl true def render(assigns) do ~H""" - + <.header> Property {@property.id} <:subtitle>This is a property record from your database. diff --git a/lib/mv_web/live/property_type_live/form.ex b/lib/mv_web/live/property_type_live/form.ex index 87cac94..8b8b452 100644 --- a/lib/mv_web/live/property_type_live/form.ex +++ b/lib/mv_web/live/property_type_live/form.ex @@ -4,7 +4,7 @@ defmodule MvWeb.PropertyTypeLive.Form do @impl true def render(assigns) do ~H""" - + <.header> {@page_title} <:subtitle> diff --git a/lib/mv_web/live/property_type_live/index.ex b/lib/mv_web/live/property_type_live/index.ex index ed9ff7d..dae4da0 100644 --- a/lib/mv_web/live/property_type_live/index.ex +++ b/lib/mv_web/live/property_type_live/index.ex @@ -4,7 +4,7 @@ defmodule MvWeb.PropertyTypeLive.Index do @impl true def render(assigns) do ~H""" - + <.header> Listing Property types <:actions> diff --git a/lib/mv_web/live/property_type_live/show.ex b/lib/mv_web/live/property_type_live/show.ex index 027baa6..ec2b0bf 100644 --- a/lib/mv_web/live/property_type_live/show.ex +++ b/lib/mv_web/live/property_type_live/show.ex @@ -4,7 +4,7 @@ defmodule MvWeb.PropertyTypeLive.Show do @impl true def render(assigns) do ~H""" - + <.header> Property type {@property_type.id} <:subtitle>This is a property_type record from your database. diff --git a/lib/mv_web/live/user_live/form.ex b/lib/mv_web/live/user_live/form.ex index e77283a..c7fd2d0 100644 --- a/lib/mv_web/live/user_live/form.ex +++ b/lib/mv_web/live/user_live/form.ex @@ -4,7 +4,7 @@ defmodule MvWeb.UserLive.Form do @impl true def render(assigns) do ~H""" - + <.header> {@page_title} <:subtitle>{gettext("Use this form to manage user records in your database.")} diff --git a/lib/mv_web/live/user_live/index.html.heex b/lib/mv_web/live/user_live/index.html.heex index 258779e..66e3b9e 100644 --- a/lib/mv_web/live/user_live/index.html.heex +++ b/lib/mv_web/live/user_live/index.html.heex @@ -1,4 +1,4 @@ - + <.header> {gettext("Listing Users")} <:actions> diff --git a/lib/mv_web/live/user_live/show.ex b/lib/mv_web/live/user_live/show.ex index 3bf6baf..609a07c 100644 --- a/lib/mv_web/live/user_live/show.ex +++ b/lib/mv_web/live/user_live/show.ex @@ -4,7 +4,7 @@ defmodule MvWeb.UserLive.Show do @impl true def render(assigns) do ~H""" - + <.header> {gettext("User")} {@user.email} <:subtitle>{gettext("This is a user record from your database.")} diff --git a/lib/mv_web/live_user_auth.ex b/lib/mv_web/live_user_auth.ex index c7b8684..8fc5224 100644 --- a/lib/mv_web/live_user_auth.ex +++ b/lib/mv_web/live_user_auth.ex @@ -28,11 +28,14 @@ defmodule MvWeb.LiveUserAuth do end end - def on_mount(:live_user_required, _params, _session, socket) do - if socket.assigns[:current_user] do - {:cont, socket} - else - {:halt, Phoenix.LiveView.redirect(socket, to: ~p"/sign-in")} + def on_mount(:live_user_required, _params, session, socket) do + socket = AshAuthentication.Phoenix.LiveSession.assign_new_resources(socket, session) + + case socket.assigns do + %{current_user: %{} = user} -> + {:cont, assign(socket, :current_user, user)} + _ -> + {:halt, Phoenix.LiveView.redirect(socket, to: ~p"/sign-in")} end end