Add authorised routes with tests

This commit is contained in:
João Gilberto Balsini Moura 2020-09-14 12:21:48 -03:00 committed by Joao Gilberto Balsini Moura
parent 37985ac1cf
commit 8590df5032
6 changed files with 131 additions and 1 deletions

View file

@ -0,0 +1,18 @@
defmodule RealEstateWeb.AdminDashboardLive do
use RealEstateWeb, :live_view
@impl true
def mount(_params, session, socket) do
socket = assign_defaults(session, socket)
{:ok, socket}
end
@impl true
def render(assigns) do
~L"""
<section class="phx-hero">
<h1>Welcome to the admin dashboard!</h1>
</section>
"""
end
end

View file

@ -0,0 +1,18 @@
defmodule RealEstateWeb.UserDashboardLive do
use RealEstateWeb, :live_view
@impl true
def mount(_params, session, socket) do
socket = assign_defaults(session, socket)
{:ok, socket}
end
@impl true
def render(assigns) do
~L"""
<section class="phx-hero">
<h1>Welcome to the user dashboard!</h1>
</section>
"""
end
end

View file

@ -1,7 +1,7 @@
defmodule RealEstateWeb.Router do
use RealEstateWeb, :router
import RealEstateWeb.UserAuth
alias RealEstateWeb.EnsureRolePlug
pipeline :browser do
plug :accepts, ["html"]
@ -17,6 +17,14 @@ defmodule RealEstateWeb.Router do
plug :accepts, ["json"]
end
pipeline :user do
plug EnsureRolePlug, [:admin, :user]
end
pipeline :admin do
plug EnsureRolePlug, :admin
end
# Other scopes may use custom stacks.
# scope "/api", RealEstateWeb do
# pipe_through :api
@ -73,4 +81,16 @@ defmodule RealEstateWeb.Router do
post "/users/confirm", UserConfirmationController, :create
get "/users/confirm/:token", UserConfirmationController, :confirm
end
scope "/", RealEstateWeb do
pipe_through [:browser, :require_authenticated_user, :user]
live "/user_dashboard", UserDashboardLive, :index
end
scope "/", RealEstateWeb do
pipe_through [:browser, :require_authenticated_user, :admin]
live "/admin_dashboard", AdminDashboardLive, :index
end
end