summaryrefslogtreecommitdiff
path: root/lib/aggiedit_web/live
diff options
context:
space:
mode:
authorLogan Hunt <loganhunt@simponic.xyz>2022-04-06 14:41:19 -0600
committerLogan Hunt <loganhunt@simponic.xyz>2022-04-06 14:41:19 -0600
commit61c2c9370a0e0139bdb9cab1de64723d60b2682c (patch)
tree5271f40facc03f4d727b268c1e3ab69f865b17dd /lib/aggiedit_web/live
parent5ad6bbfce753e048a4f866ad6324f4b4d6f16618 (diff)
downloadaggiedit-61c2c9370a0e0139bdb9cab1de64723d60b2682c.tar.gz
aggiedit-61c2c9370a0e0139bdb9cab1de64723d60b2682c.zip
Fix authentication on posts page
Diffstat (limited to 'lib/aggiedit_web/live')
-rw-r--r--lib/aggiedit_web/live/live_helpers.ex12
-rw-r--r--lib/aggiedit_web/live/post_live/index.ex8
2 files changed, 18 insertions, 2 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