dev-resources.site
for different kinds of informations.
What you should know about the live_session macro
Published at
12/5/2023
Categories
elixir
erlang
phoenix
liveview
Author
Herminio Torres
Imagine you have a few endpoints and would like to group their authorization rules. With live_session/3, can achieve that!
live_session
has three options:
-
session
- name of the session -
on_mount
- callback function -
root_layout
- apply a different layout to the group
It is important to understand the Security Considerations of live_session
, especially for handling authentication and authorization in your LiveView.
In the following example, we use live_session to set a new root_layout only for admin users and authorize admins only in the :admin
UserHook
live_session :admins,
root_layout: {ExampleWeb.AdminLayoutView, :root},
on_mount: {ExampleWeb.UserHook, :admin} do
scope "/", ExampleWeb do
pipe_through [:browser, :auth]
live "/admin", HomeLive, :page
end
end
defmodule ExampleWeb.AdminLayoutView do
@moduledoc false
use ExampleWeb, :view
def render("root.html", assigns) do
~H"""
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Layout</title>
</head>
<body>
<h1>Admin</h1>
<main>
<%= @inner_content %>
</main>
</body>
</html>
"""
end
end
Articles
12 articles in total
Bridging the Gap: Simplifying Live Component Invocation in Phoenix LiveView
read article
Taming data with Ecto.Enum and Ecto.Type
read article
Troubleshooting MacOS Performance: Addressing fseventsd Issues Causing Memory Leaks and Sluggish Performance
read article
Exploring the Data Analysis: From Python Certification to the Elixir Challenge - Mean-Variance-Standard Deviation Calculator
read article
Using the Keyword module for options
read article
How to import CSV file to the Database
read article
How to take leverage from on_mount to reduce code
read article
What you should know about the live_session macro
currently reading
When to use the handle_params callback
read article
How to use unique_index wisely to grasp our business logic
read article
Understanding Process Restart Strategies: Transient, Temporary, and Permanent
read article
Managing Timeouts in GenServer in Elixir: How to Control Waiting Time in Critical Operations
read article
Featured ones: