diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-06 14:41:19 -0600 |
---|---|---|
committer | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-06 14:41:19 -0600 |
commit | 61c2c9370a0e0139bdb9cab1de64723d60b2682c (patch) | |
tree | 5271f40facc03f4d727b268c1e3ab69f865b17dd | |
parent | 5ad6bbfce753e048a4f866ad6324f4b4d6f16618 (diff) | |
download | aggiedit-61c2c9370a0e0139bdb9cab1de64723d60b2682c.tar.gz aggiedit-61c2c9370a0e0139bdb9cab1de64723d60b2682c.zip |
Fix authentication on posts page
-rw-r--r-- | lib/aggiedit_web/live/live_helpers.ex | 12 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/index.ex | 8 | ||||
-rw-r--r-- | lib/aggiedit_web/router.ex | 3 | ||||
-rw-r--r-- | lib/aggiedit_web/templates/layout/root.html.heex | 3 |
4 files changed, 21 insertions, 5 deletions
diff --git a/lib/aggiedit_web/live/live_helpers.ex b/lib/aggiedit_web/live/live_helpers.ex index aa6b28a..e31fedc 100644 --- a/lib/aggiedit_web/live/live_helpers.ex +++ b/lib/aggiedit_web/live/live_helpers.ex @@ -4,6 +4,9 @@ defmodule AggieditWeb.LiveHelpers do alias Phoenix.LiveView.JS + alias Aggiedit.Accounts + alias Aggiedit.Accounts.User + @doc """ Renders a live component inside a modal. @@ -57,4 +60,13 @@ defmodule AggieditWeb.LiveHelpers do |> JS.hide(to: "#modal", transition: "fade-out") |> JS.hide(to: "#modal-content", transition: "fade-out-scale") end + + def assign_socket_user(session, socket) do + with token when not is_nil(token) <- session["user_token"], + current_user=%User{} = Accounts.get_user_by_session_token(token) do + assign(socket, :current_user, current_user) + else + _ -> socket + end + end end diff --git a/lib/aggiedit_web/live/post_live/index.ex b/lib/aggiedit_web/live/post_live/index.ex index 0b12ed0..1bf7bc3 100644 --- a/lib/aggiedit_web/live/post_live/index.ex +++ b/lib/aggiedit_web/live/post_live/index.ex @@ -5,8 +5,12 @@ defmodule AggieditWeb.PostLive.Index do alias Aggiedit.Rooms.Post @impl true - def mount(_params, _session, socket) do - {:ok, assign(socket, :posts, list_posts())} + def mount(_params, session, socket) do + socket = assign_socket_user(session, socket) + case socket.assigns do + %{:current_user => user} -> {:ok, assign(socket, :posts, list_posts())} + _ -> {:ok, socket |> put_flash(:error, "You must log in to access this page.") |> redirect(to: Routes.user_session_path(socket, :new))} + end end @impl true diff --git a/lib/aggiedit_web/router.ex b/lib/aggiedit_web/router.ex index dec730a..99aee90 100644 --- a/lib/aggiedit_web/router.ex +++ b/lib/aggiedit_web/router.ex @@ -21,7 +21,10 @@ defmodule AggieditWeb.Router do pipe_through :browser get "/", PageController, :index + end + scope "/", AggieditWeb do + pipe_through [:browser, :require_authenticated_user] live "/posts", PostLive.Index, :index live "/posts/new", PostLive.Index, :new live "/posts/:id/edit", PostLive.Index, :edit diff --git a/lib/aggiedit_web/templates/layout/root.html.heex b/lib/aggiedit_web/templates/layout/root.html.heex index f653fd8..014c44a 100644 --- a/lib/aggiedit_web/templates/layout/root.html.heex +++ b/lib/aggiedit_web/templates/layout/root.html.heex @@ -21,9 +21,6 @@ </ul> <%= render "_user_menu.html", assigns %> </nav> - <a href="https://phoenixframework.org/" class="phx-logo"> - <img src={Routes.static_path(@conn, "/images/phoenix.png")} alt="Phoenix Framework Logo"/> - </a> </section> </header> <%= @inner_content %> |