Skip to content

Commit

Permalink
Move loggedin assign to router plug
Browse files Browse the repository at this point in the history
Create a router plug to assign true/false to loggedin.
This to avoid having duplicated code on the controllers
  • Loading branch information
SimonLab committed Oct 5, 2022
1 parent 211708f commit 6797530
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 20 deletions.
9 changes: 0 additions & 9 deletions lib/app_web/controllers/profile_controller.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule AppWeb.ProfileController do
use AppWeb, :controller
alias App.Person
plug :loggedin
plug :permission_profile when action in [:show, :edit, :update]

def show(conn, %{"personid" => person_id}) do
Expand All @@ -16,14 +15,6 @@ defmodule AppWeb.ProfileController do
render(conn, "edit.html", profile: profile, changeset: changeset)
end

defp loggedin(conn, _opts) do
if not is_nil(conn.assigns[:jwt]) do
assign(conn, :loggedin, true)
else
assign(conn, :loggedin, false)
end
end

defp permission_profile(conn, _opts) do
person_id = conn.assigns[:person][:id] || 0

Expand Down
9 changes: 0 additions & 9 deletions lib/app_web/controllers/tag_controller.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule AppWeb.TagController do
use AppWeb, :controller
alias App.Tag
plug :loggedin
plug :permission_tag when action in [:edit, :update, :delete]

def index(conn, _params) do
Expand Down Expand Up @@ -41,14 +40,6 @@ defmodule AppWeb.TagController do
|> redirect(to: Routes.tag_path(conn, :index))
end

defp loggedin(conn, _opts) do
if not is_nil(conn.assigns[:jwt]) do
assign(conn, :loggedin, true)
else
assign(conn, :loggedin, false)
end
end

defp permission_tag(conn, _opts) do
tag = Tag.get_tag!(conn.params["id"])

Expand Down
11 changes: 10 additions & 1 deletion lib/app_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ defmodule AppWeb.Router do
end

pipeline :authOptional, do: plug(AuthPlugOptional)
pipeline :verify_loggedin, do: plug(:loggedin)

scope "/", AppWeb do
pipe_through [:browser, :authOptional]
pipe_through [:browser, :authOptional, :verify_loggedin]

live "/", AppLive
resources "/tags", TagController, except: [:show]
Expand All @@ -30,4 +31,12 @@ defmodule AppWeb.Router do
except: [:index, :delete],
param: "person_id"
end

defp loggedin(conn, _opts) do
if not is_nil(conn.assigns[:jwt]) do
assign(conn, :loggedin, true)
else
assign(conn, :loggedin, false)
end
end
end
1 change: 0 additions & 1 deletion lib/app_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<a href={Routes.profile_path(@conn, :edit, @person.id)}>
<img src={@person.picture} class="mr-3 h-6 sm:h-9 rounded-full" alt="avatar image">
</a>
<%= Routes.profile_path(@conn, :show, @person.id) %>
<% else %>
<h1 class="text-white">Hi Friend!</h1>
<% end %>
Expand Down

0 comments on commit 6797530

Please sign in to comment.